1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* pg_type.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines to support manipulation of the pg_type relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-01-23 05:32:23 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_type.c,v 1.57 2001/01/23 04:32:21 tgl 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_type.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "parser/parse_func.h"
|
|
|
|
#include "utils/builtins.h"
|
2000-05-28 19:56:29 +02:00
|
|
|
#include "utils/fmgroids.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
1996-11-04 00:27:08 +01:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static Oid TypeShellMakeWithOpenRelation(Relation pg_type_desc,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *typeName);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* TypeGetWithOpenRelation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* preforms a scan on pg_type for a type tuple with the
|
|
|
|
* given type name.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pg_type_desc -- reldesc for pg_type
|
|
|
|
* typeName -- name of type to be fetched
|
|
|
|
* defined -- has the type been defined?
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Oid
|
1996-07-09 08:22:35 +02:00
|
|
|
TypeGetWithOpenRelation(Relation pg_type_desc,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *typeName,
|
1997-09-08 23:56:23 +02:00
|
|
|
bool *defined)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple tup;
|
1999-02-06 17:50:34 +01:00
|
|
|
Oid typoid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
static ScanKeyData typeKey[1] = {
|
1998-04-27 06:08:07 +02:00
|
|
|
{0, Anum_pg_type_typname, F_NAMEEQ}
|
1997-09-07 07:04:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* initialize the scan key and begin a scan of pg_type
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-04-27 06:08:07 +02:00
|
|
|
fmgr_info(F_NAMEEQ, &typeKey[0].sk_func);
|
1998-01-15 20:46:37 +01:00
|
|
|
typeKey[0].sk_nargs = typeKey[0].sk_func.fn_nargs;
|
1997-09-07 07:04:48 +02:00
|
|
|
typeKey[0].sk_argument = PointerGetDatum(typeName);
|
|
|
|
|
|
|
|
scan = heap_beginscan(pg_type_desc,
|
|
|
|
0,
|
1998-08-19 04:04:17 +02:00
|
|
|
SnapshotSelf, /* cache? */
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
typeKey);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* get the type tuple, if it exists.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
tup = heap_getnext(scan, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if no type tuple exists for the given type name, then
|
|
|
|
* end the scan and return appropriate information.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
{
|
|
|
|
heap_endscan(scan);
|
|
|
|
*defined = false;
|
|
|
|
return InvalidOid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* here, the type tuple does exist so we pull information from
|
|
|
|
* the typisdefined field of the tuple and return the tuple's
|
|
|
|
* oid, which is the oid of the type.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-09-01 05:29:17 +02:00
|
|
|
*defined = (bool) ((Form_pg_type) GETSTRUCT(tup))->typisdefined;
|
1999-02-06 17:50:34 +01:00
|
|
|
typoid = tup->t_data->t_oid;
|
|
|
|
|
|
|
|
heap_endscan(scan);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-06 17:50:34 +01:00
|
|
|
return typoid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* TypeGet
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Finds the ObjectId of a type, even if uncommitted; "defined"
|
|
|
|
* is only set if the type has actually been defined, i.e., if
|
|
|
|
* the type tuple is not a shell.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note: the meat of this function is now in the function
|
|
|
|
* TypeGetWithOpenRelation(). -cim 6/15/90
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Also called from util/remove.c
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Oid
|
1997-09-07 07:04:48 +02:00
|
|
|
TypeGet(char *typeName, /* name of type to be fetched */
|
1997-09-08 23:56:23 +02:00
|
|
|
bool *defined) /* has the type been defined? */
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_type_desc;
|
|
|
|
Oid typeoid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* open the pg_type relation
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_type_desc = heap_openr(TypeRelationName, AccessShareLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* scan the type relation for the information we want
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
typeoid = TypeGetWithOpenRelation(pg_type_desc,
|
|
|
|
typeName,
|
|
|
|
defined);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* close the type relation and return the type oid.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_type_desc, AccessShareLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
return typeoid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* TypeShellMakeWithOpenRelation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Oid
|
1996-07-09 08:22:35 +02:00
|
|
|
TypeShellMakeWithOpenRelation(Relation pg_type_desc, char *typeName)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
int i;
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
Datum values[Natts_pg_type];
|
|
|
|
char nulls[Natts_pg_type];
|
|
|
|
Oid typoid;
|
1998-04-01 17:35:33 +02:00
|
|
|
NameData name;
|
1997-09-08 04:41:22 +02:00
|
|
|
TupleDesc tupDesc;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1998-09-01 05:29:17 +02:00
|
|
|
* initialize our *nulls and *values arrays
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
for (i = 0; i < Natts_pg_type; ++i)
|
|
|
|
{
|
|
|
|
nulls[i] = ' ';
|
|
|
|
values[i] = (Datum) NULL; /* redundant, but safe */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
2000-07-06 01:12:09 +02:00
|
|
|
* initialize *values with the type name and dummy values
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
i = 0;
|
1998-04-01 17:35:33 +02:00
|
|
|
namestrcpy(&name, typeName);
|
2000-07-06 01:12:09 +02:00
|
|
|
values[i++] = NameGetDatum(&name); /* 1 */
|
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 2 */
|
|
|
|
values[i++] = Int16GetDatum(0); /* 3 */
|
|
|
|
values[i++] = Int16GetDatum(0); /* 4 */
|
|
|
|
values[i++] = BoolGetDatum(false); /* 5 */
|
2000-08-21 19:22:36 +02:00
|
|
|
values[i++] = CharGetDatum(0); /* 6 */
|
2000-07-06 01:12:09 +02:00
|
|
|
values[i++] = BoolGetDatum(false); /* 7 */
|
2000-08-21 19:22:36 +02:00
|
|
|
values[i++] = CharGetDatum(0); /* 8 */
|
2000-07-06 01:12:09 +02:00
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 9 */
|
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 10 */
|
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 11 */
|
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 12 */
|
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 13 */
|
|
|
|
values[i++] = ObjectIdGetDatum(InvalidOid); /* 14 */
|
2000-08-21 19:22:36 +02:00
|
|
|
values[i++] = CharGetDatum('i'); /* 15 */
|
|
|
|
values[i++] = CharGetDatum('p'); /* 16 */
|
2000-07-06 01:12:09 +02:00
|
|
|
values[i++] = DirectFunctionCall1(textin,
|
|
|
|
CStringGetDatum(typeName)); /* 17 */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* create a new type tuple with FormHeapTuple
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
tupDesc = pg_type_desc->rd_att;
|
|
|
|
|
|
|
|
tup = heap_formtuple(tupDesc, values, nulls);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* insert the tuple in the relation and get the tuple's oid.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
heap_insert(pg_type_desc, tup);
|
1998-11-27 20:52:36 +01:00
|
|
|
typoid = tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
if (RelationGetForm(pg_type_desc)->relhasindex)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation idescs[Num_pg_type_indices];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
CatalogOpenIndices(Num_pg_type_indices, Name_pg_type_indices, idescs);
|
|
|
|
CatalogIndexInsert(idescs, Num_pg_type_indices, pg_type_desc, tup);
|
|
|
|
CatalogCloseIndices(Num_pg_type_indices, idescs);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* free the tuple and return the type-oid
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-03 22:18:02 +01:00
|
|
|
return typoid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* TypeShellMake
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This procedure inserts a "shell" tuple into the type
|
|
|
|
* relation. The type tuple inserted has invalid values
|
|
|
|
* and in particular, the "typisdefined" field is false.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This is used so that a tuple exists in the catalogs.
|
|
|
|
* The invalid fields should be fixed up sometime after
|
|
|
|
* this routine is called, and then the "typeisdefined"
|
|
|
|
* field is set to true. -cim 6/15/90
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
TypeShellMake(char *typeName)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_type_desc;
|
|
|
|
Oid typoid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(PointerIsValid(typeName));
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* open pg_type
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_type_desc = heap_openr(TypeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* insert the shell tuple
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
typoid = TypeShellMakeWithOpenRelation(pg_type_desc, typeName);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* close pg_type and return the tuple's oid.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_type_desc, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-03 22:18:02 +01:00
|
|
|
return typoid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* TypeCreate
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This does all the necessary work needed to define a new type.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
TypeCreate(char *typeName,
|
1997-09-07 07:04:48 +02:00
|
|
|
Oid relationOid, /* only for 'c'atalog typeTypes */
|
|
|
|
int16 internalSize,
|
|
|
|
int16 externalSize,
|
|
|
|
char typeType,
|
|
|
|
char typDelim,
|
|
|
|
char *inputProcedure,
|
|
|
|
char *outputProcedure,
|
|
|
|
char *receiveProcedure,
|
1997-11-26 05:50:47 +01:00
|
|
|
char *sendProcedure,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *elementTypeName,
|
|
|
|
char *defaultTypeValue, /* internal rep */
|
|
|
|
bool passedByValue,
|
2000-07-04 01:10:14 +02:00
|
|
|
char alignment,
|
|
|
|
char storage)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
int i,
|
1997-09-08 04:41:22 +02:00
|
|
|
j;
|
|
|
|
Relation pg_type_desc;
|
|
|
|
HeapScanDesc pg_type_scan;
|
|
|
|
|
|
|
|
Oid typeObjectId;
|
|
|
|
Oid elementObjectId = InvalidOid;
|
|
|
|
|
|
|
|
HeapTuple tup;
|
|
|
|
char nulls[Natts_pg_type];
|
|
|
|
char replaces[Natts_pg_type];
|
|
|
|
Datum values[Natts_pg_type];
|
|
|
|
|
|
|
|
char *procname;
|
|
|
|
char *procs[4];
|
|
|
|
bool defined;
|
1998-04-01 17:35:33 +02:00
|
|
|
NameData name;
|
1997-09-08 04:41:22 +02:00
|
|
|
TupleDesc tupDesc;
|
2000-01-10 18:14:46 +01:00
|
|
|
Oid argList[FUNC_MAX_ARGS];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
static ScanKeyData typeKey[1] = {
|
1998-04-27 06:08:07 +02:00
|
|
|
{0, Anum_pg_type_typname, F_NAMEEQ}
|
1997-09-07 07:04:48 +02:00
|
|
|
};
|
|
|
|
|
1998-04-27 06:08:07 +02:00
|
|
|
fmgr_info(F_NAMEEQ, &typeKey[0].sk_func);
|
1998-01-15 20:46:37 +01:00
|
|
|
typeKey[0].sk_nargs = typeKey[0].sk_func.fn_nargs;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* check that the type is not already defined.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
typeObjectId = TypeGet(typeName, &defined);
|
|
|
|
if (OidIsValid(typeObjectId) && defined)
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "TypeCreate: type %s already defined", typeName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if this type has an associated elementType, then we check that
|
|
|
|
* it is defined.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (elementTypeName)
|
|
|
|
{
|
|
|
|
elementObjectId = TypeGet(elementTypeName, &defined);
|
|
|
|
if (!defined)
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "TypeCreate: type %s is not defined", elementTypeName);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* XXX comment me
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (externalSize == 0)
|
|
|
|
externalSize = -1; /* variable length */
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* initialize arrays needed by FormHeapTuple
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
for (i = 0; i < Natts_pg_type; ++i)
|
|
|
|
{
|
|
|
|
nulls[i] = ' ';
|
|
|
|
replaces[i] = 'r';
|
|
|
|
values[i] = (Datum) NULL; /* redundant, but nice */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
*
|
|
|
|
* Do this so that user-defined types have size -1 instead of zero if
|
|
|
|
* they are variable-length - this is so that everything else in the
|
|
|
|
* backend works.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (internalSize == 0)
|
|
|
|
internalSize = -1;
|
|
|
|
|
|
|
|
/* ----------------
|
1998-09-01 05:29:17 +02:00
|
|
|
* initialize the *values information
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
i = 0;
|
1998-09-01 06:40:42 +02:00
|
|
|
namestrcpy(&name, typeName);
|
2000-08-21 19:22:36 +02:00
|
|
|
values[i++] = NameGetDatum(&name); /* 1 */
|
|
|
|
values[i++] = Int32GetDatum(GetUserId()); /* 2 */
|
|
|
|
values[i++] = Int16GetDatum(internalSize); /* 3 */
|
|
|
|
values[i++] = Int16GetDatum(externalSize); /* 4 */
|
|
|
|
values[i++] = BoolGetDatum(passedByValue); /* 5 */
|
|
|
|
values[i++] = CharGetDatum(typeType); /* 6 */
|
|
|
|
values[i++] = BoolGetDatum(true); /* 7 */
|
|
|
|
values[i++] = CharGetDatum(typDelim); /* 8 */
|
|
|
|
values[i++] = ObjectIdGetDatum(typeType == 'c' ? relationOid : InvalidOid); /* 9 */
|
|
|
|
values[i++] = ObjectIdGetDatum(elementObjectId); /* 10 */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
procs[0] = inputProcedure;
|
|
|
|
procs[1] = outputProcedure;
|
|
|
|
procs[2] = (receiveProcedure) ? receiveProcedure : inputProcedure;
|
|
|
|
procs[3] = (sendProcedure) ? sendProcedure : outputProcedure;
|
|
|
|
|
|
|
|
for (j = 0; j < 4; ++j)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
Oid procOid;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
procname = procs[j];
|
|
|
|
|
1999-04-20 05:51:19 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* First look for a 1-argument func with all argtypes 0. This is
|
|
|
|
* valid for all four kinds of procedure.
|
1999-04-20 05:51:19 +02:00
|
|
|
*/
|
2000-01-10 18:14:46 +01:00
|
|
|
MemSet(argList, 0, FUNC_MAX_ARGS * sizeof(Oid));
|
1999-04-20 05:51:19 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
procOid = GetSysCacheOid(PROCNAME,
|
|
|
|
PointerGetDatum(procname),
|
|
|
|
Int32GetDatum(1),
|
|
|
|
PointerGetDatum(argList),
|
|
|
|
0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
if (!OidIsValid(procOid))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* For array types, the input procedures may take 3 args (data
|
|
|
|
* value, element OID, atttypmod); the pg_proc argtype
|
2000-06-13 09:35:40 +02:00
|
|
|
* signature is 0,OIDOID,INT4OID. The output procedures may
|
|
|
|
* take 2 args (data value, element OID).
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (OidIsValid(elementObjectId))
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int nargs;
|
|
|
|
|
1999-04-20 05:51:19 +02:00
|
|
|
if (j % 2)
|
|
|
|
{
|
|
|
|
/* output proc */
|
|
|
|
nargs = 2;
|
2000-06-13 09:35:40 +02:00
|
|
|
argList[1] = OIDOID;
|
1999-05-25 18:15:34 +02:00
|
|
|
}
|
|
|
|
else
|
1999-04-20 05:51:19 +02:00
|
|
|
{
|
|
|
|
/* input proc */
|
|
|
|
nargs = 3;
|
2000-06-13 09:35:40 +02:00
|
|
|
argList[1] = OIDOID;
|
1999-04-20 05:51:19 +02:00
|
|
|
argList[2] = INT4OID;
|
|
|
|
}
|
2000-11-16 23:30:52 +01:00
|
|
|
procOid = GetSysCacheOid(PROCNAME,
|
|
|
|
PointerGetDatum(procname),
|
|
|
|
Int32GetDatum(nargs),
|
|
|
|
PointerGetDatum(argList),
|
|
|
|
0);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-11-16 23:30:52 +01:00
|
|
|
if (!OidIsValid(procOid))
|
1998-05-10 01:43:45 +02:00
|
|
|
func_error("TypeCreate", procname, 1, argList, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
values[i++] = ObjectIdGetDatum(procOid); /* 11 - 14 */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* set default alignment
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-08-21 19:22:36 +02:00
|
|
|
values[i++] = CharGetDatum(alignment); /* 15 */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
/* ----------------
|
|
|
|
* set default storage for TOAST
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-08-21 19:22:36 +02:00
|
|
|
values[i++] = CharGetDatum(storage); /* 16 */
|
2000-07-04 01:10:14 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* initialize the default value for this type.
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-07-06 01:12:09 +02:00
|
|
|
values[i] = DirectFunctionCall1(textin, /* 17 */
|
|
|
|
CStringGetDatum(defaultTypeValue ? defaultTypeValue : "-"));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* open pg_type and begin a scan for the type name.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_type_desc = heap_openr(TypeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
typeKey[0].sk_argument = PointerGetDatum(typeName);
|
|
|
|
pg_type_scan = heap_beginscan(pg_type_desc,
|
|
|
|
0,
|
1998-08-19 04:04:17 +02:00
|
|
|
SnapshotSelf, /* cache? */
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
typeKey);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* define the type either by adding a tuple to the type
|
|
|
|
* relation, or by updating the fields of the "shell" tuple
|
|
|
|
* already there.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
tup = heap_getnext(pg_type_scan, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (HeapTupleIsValid(tup))
|
|
|
|
{
|
|
|
|
tup = heap_modifytuple(tup,
|
|
|
|
pg_type_desc,
|
|
|
|
values,
|
|
|
|
nulls,
|
|
|
|
replaces);
|
|
|
|
|
2001-01-23 05:32:23 +01:00
|
|
|
simple_heap_update(pg_type_desc, &tup->t_self, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-11-27 20:52:36 +01:00
|
|
|
typeObjectId = tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tupDesc = pg_type_desc->rd_att;
|
|
|
|
|
|
|
|
tup = heap_formtuple(tupDesc,
|
|
|
|
values,
|
|
|
|
nulls);
|
|
|
|
|
|
|
|
heap_insert(pg_type_desc, tup);
|
|
|
|
|
1998-11-27 20:52:36 +01:00
|
|
|
typeObjectId = tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* finish up
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
heap_endscan(pg_type_scan);
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
if (RelationGetForm(pg_type_desc)->relhasindex)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation idescs[Num_pg_type_indices];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
CatalogOpenIndices(Num_pg_type_indices, Name_pg_type_indices, idescs);
|
|
|
|
CatalogIndexInsert(idescs, Num_pg_type_indices, pg_type_desc, tup);
|
|
|
|
CatalogCloseIndices(Num_pg_type_indices, idescs);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-09-18 21:08:25 +02:00
|
|
|
|
|
|
|
heap_close(pg_type_desc, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
return typeObjectId;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* TypeRename
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This renames a type
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2000-01-22 15:20:56 +01:00
|
|
|
TypeRename(const char *oldTypeName, const char *newTypeName)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_type_desc;
|
|
|
|
Relation idescs[Num_pg_type_indices];
|
2000-11-16 23:30:52 +01:00
|
|
|
HeapTuple tuple;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_type_desc = heap_openr(TypeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCacheCopy(TYPENAME,
|
|
|
|
PointerGetDatum(oldTypeName),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
elog(ERROR, "TypeRename: type \"%s\" not defined", oldTypeName);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
if (SearchSysCacheExists(TYPENAME,
|
|
|
|
PointerGetDatum(newTypeName),
|
|
|
|
0, 0, 0))
|
|
|
|
elog(ERROR, "TypeRename: type \"%s\" already defined", newTypeName);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
namestrcpy(&(((Form_pg_type) GETSTRUCT(tuple))->typname), newTypeName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-01-23 05:32:23 +01:00
|
|
|
simple_heap_update(pg_type_desc, &tuple->t_self, tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
/* update the system catalog indices */
|
|
|
|
CatalogOpenIndices(Num_pg_type_indices, Name_pg_type_indices, idescs);
|
2000-11-16 23:30:52 +01:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_type_indices, pg_type_desc, tuple);
|
1998-08-19 04:04:17 +02:00
|
|
|
CatalogCloseIndices(Num_pg_type_indices, idescs);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
heap_freetuple(tuple);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_type_desc, RowExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* makeArrayTypeName(typeName);
|
1997-09-07 07:04:48 +02:00
|
|
|
* - given a base type name, make an array of type name out of it
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* the CALLER is responsible for pfreeing the
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1997-09-07 07:04:48 +02:00
|
|
|
makeArrayTypeName(char *typeName)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *arr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!typeName)
|
|
|
|
return NULL;
|
|
|
|
arr = palloc(strlen(typeName) + 2);
|
|
|
|
arr[0] = '_';
|
|
|
|
strcpy(arr + 1, typeName);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return arr;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
}
|