Skip to content

Commit d5e99ab

Browse files
committed
pg_type has a typnamespace column; system now supports creating types
in different namespaces. Also, cleanup work on relation namespace support: drop, alter, rename commands work for tables in non-default namespaces.
1 parent 7c1ff35 commit d5e99ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2069
-2261
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
Documentation of the system catalogs, directed toward PostgreSQL developers
3-
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.38 2002/03/26 19:15:10 tgl Exp $
3+
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.39 2002/03/29 19:05:57 tgl Exp $
44
-->
55

66
<chapter id="catalogs">
@@ -2363,6 +2363,15 @@
23632363
<entry>Data type name</entry>
23642364
</row>
23652365

2366+
<row>
2367+
<entry>typnamespace</entry>
2368+
<entry><type>oid</type></entry>
2369+
<entry>pg_namespace.oid</entry>
2370+
<entry>
2371+
The OID of the namespace that contains this type
2372+
</entry>
2373+
</row>
2374+
23662375
<row>
23672376
<entry>typowner</entry>
23682377
<entry><type>int4</type></entry>

src/backend/access/common/tupdesc.c

Lines changed: 11 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/common/tupdesc.c,v 1.77 2002/02/27 19:34:11 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/common/tupdesc.c,v 1.78 2002/03/29 19:05:59 tgl Exp $
1212
*
1313
* NOTES
1414
* some of the executor utility code such as "ExecTypeFromTL" should be
@@ -322,7 +322,7 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
322322
* a preallocated tuple descriptor.
323323
* ----------------------------------------------------------------
324324
*/
325-
bool
325+
void
326326
TupleDescInitEntry(TupleDesc desc,
327327
AttrNumber attributeNumber,
328328
char *attributeName,
@@ -377,39 +377,11 @@ TupleDescInitEntry(TupleDesc desc,
377377
att->attnotnull = false;
378378
att->atthasdef = false;
379379

380-
/* ----------------
381-
* search the system cache for the type tuple of the attribute
382-
* we are creating so that we can get the typeid and some other
383-
* stuff.
384-
*
385-
* Note: in the special case of
386-
*
387-
* create EMP (name = text, manager = EMP)
388-
*
389-
* RelationNameCreateHeapRelation() calls BuildDesc() which
390-
* calls this routine and since EMP does not exist yet, the
391-
* system cache lookup below fails. That's fine, but rather
392-
* then doing a elog(ERROR) we just leave that information
393-
* uninitialized, return false, then fix things up later.
394-
* -cim 6/14/90
395-
* ----------------
396-
*/
397380
tuple = SearchSysCache(TYPEOID,
398381
ObjectIdGetDatum(oidtypeid),
399382
0, 0, 0);
400383
if (!HeapTupleIsValid(tuple))
401-
{
402-
/*
403-
* here type info does not exist yet so we just fill the attribute
404-
* with dummy information and return false.
405-
*/
406-
att->atttypid = InvalidOid;
407-
att->attlen = (int16) 0;
408-
att->attbyval = (bool) 0;
409-
att->attalign = 'i';
410-
att->attstorage = 'p';
411-
return false;
412-
}
384+
elog(ERROR, "Unable to look up type id %u", oidtypeid);
413385

414386
/*
415387
* type info exists so we initialize our attribute information from
@@ -477,56 +449,16 @@ TupleDescInitEntry(TupleDesc desc,
477449
}
478450

479451
ReleaseSysCache(tuple);
480-
481-
return true;
482452
}
483453

484454

485-
/* ----------------------------------------------------------------
486-
* TupleDescMakeSelfReference
455+
/*
456+
* BuildDescForRelation
487457
*
488-
* This function initializes a "self-referential" attribute like
489-
* manager in "create EMP (name=text, manager = EMP)".
490-
* It calls TypeShellMake() which inserts a "shell" type
491-
* tuple into pg_type. A self-reference is one kind of set, so
492-
* its size and byval are the same as for a set. See the comments
493-
* above in TupleDescInitEntry.
494-
* ----------------------------------------------------------------
495-
*/
496-
static void
497-
TupleDescMakeSelfReference(TupleDesc desc,
498-
AttrNumber attnum,
499-
char *relname)
500-
{
501-
Form_pg_attribute att;
502-
503-
att = desc->attrs[attnum - 1];
504-
att->atttypid = TypeShellMake(relname);
505-
att->attlen = sizeof(Oid);
506-
att->attbyval = true;
507-
att->attalign = 'i';
508-
att->attstorage = 'p';
509-
att->attndims = 0;
510-
}
511-
512-
/* ----------------------------------------------------------------
513-
* BuildDescForRelation
514-
*
515-
* This is a general purpose function identical to BuildDesc
516-
* but is used by the DefineRelation() code to catch the
517-
* special case where you
518-
*
519-
* create FOO ( ..., x = FOO )
520-
*
521-
* here, the initial type lookup for "x = FOO" will fail
522-
* because FOO isn't in the catalogs yet. But since we
523-
* are creating FOO, instead of doing an elog() we add
524-
* a shell type tuple to pg_type and fix things later
525-
* in amcreate().
526-
* ----------------------------------------------------------------
458+
* Given a relation schema (list of ColumnDef nodes), build a TupleDesc.
527459
*/
528460
TupleDesc
529-
BuildDescForRelation(List *schema, char *relname)
461+
BuildDescForRelation(List *schema)
530462
{
531463
int natts;
532464
AttrNumber attnum;
@@ -535,7 +467,6 @@ BuildDescForRelation(List *schema, char *relname)
535467
AttrDefault *attrdef = NULL;
536468
TupleConstr *constr = (TupleConstr *) palloc(sizeof(TupleConstr));
537469
char *attname;
538-
char typename[NAMEDATALEN];
539470
int32 atttypmod;
540471
int attdim;
541472
int ndef = 0;
@@ -553,7 +484,6 @@ BuildDescForRelation(List *schema, char *relname)
553484
foreach(p, schema)
554485
{
555486
ColumnDef *entry = lfirst(p);
556-
List *arry;
557487

558488
/*
559489
* for each entry in the list, get the name and type information
@@ -563,39 +493,13 @@ BuildDescForRelation(List *schema, char *relname)
563493
attnum++;
564494

565495
attname = entry->colname;
566-
arry = entry->typename->arrayBounds;
567496
attisset = entry->typename->setof;
568497
atttypmod = entry->typename->typmod;
498+
attdim = length(entry->typename->arrayBounds);
569499

570-
if (arry != NIL)
571-
{
572-
/* array of XXX is _XXX */
573-
snprintf(typename, NAMEDATALEN,
574-
"_%.*s", NAMEDATALEN - 2, entry->typename->name);
575-
attdim = length(arry);
576-
}
577-
else
578-
{
579-
StrNCpy(typename, entry->typename->name, NAMEDATALEN);
580-
attdim = 0;
581-
}
582-
583-
if (!TupleDescInitEntry(desc, attnum, attname,
584-
typenameTypeId(typename),
585-
atttypmod, attdim, attisset))
586-
{
587-
/*
588-
* if TupleDescInitEntry() fails, it means there is no type in
589-
* the system catalogs. So now we check if the type name
590-
* equals the relation name. If so we have a self reference,
591-
* otherwise it's an error.
592-
*/
593-
if (strcmp(typename, relname) == 0)
594-
TupleDescMakeSelfReference(desc, attnum, relname);
595-
else
596-
elog(ERROR, "DefineRelation: no such type %s",
597-
typename);
598-
}
500+
TupleDescInitEntry(desc, attnum, attname,
501+
typenameTypeId(entry->typename),
502+
atttypmod, attdim, attisset);
599503

600504
/* This is for constraints */
601505
if (entry->is_not_null)

src/backend/catalog/aclchk.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.59 2002/03/26 19:15:22 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.60 2002/03/29 19:05:59 tgl Exp $
1212
*
1313
* NOTES
1414
* See acl.h.
@@ -37,6 +37,7 @@
3737
#include "parser/parse_agg.h"
3838
#include "parser/parse_func.h"
3939
#include "parser/parse_expr.h"
40+
#include "parser/parse_type.h"
4041
#include "utils/acl.h"
4142
#include "utils/syscache.h"
4243
#include "utils/temprel.h"
@@ -300,20 +301,19 @@ find_function_with_arglist(char *name, List *arguments)
300301
for (i = 0; i < argcount; i++)
301302
{
302303
TypeName *t = (TypeName *) lfirst(arguments);
303-
char *typnam = TypeNameToInternalName(t);
304304

305-
arguments = lnext(arguments);
306-
307-
if (strcmp(typnam, "opaque") == 0)
308-
argoids[i] = InvalidOid;
309-
else
305+
argoids[i] = LookupTypeName(t);
306+
if (!OidIsValid(argoids[i]))
310307
{
311-
argoids[i] = GetSysCacheOid(TYPENAME,
312-
PointerGetDatum(typnam),
313-
0, 0, 0);
314-
if (!OidIsValid(argoids[i]))
315-
elog(ERROR, "type '%s' not found", typnam);
308+
char *typnam = TypeNameToString(t);
309+
310+
if (strcmp(typnam, "opaque") == 0)
311+
argoids[i] = InvalidOid;
312+
else
313+
elog(ERROR, "Type \"%s\" does not exist", typnam);
316314
}
315+
316+
arguments = lnext(arguments);
317317
}
318318

319319
oid = GetSysCacheOid(PROCNAME,

src/backend/catalog/heap.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.192 2002/03/26 19:15:25 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.193 2002/03/29 19:05:59 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -74,8 +74,10 @@ static void DeleteRelationTuple(Relation rel);
7474
static void DeleteTypeTuple(Relation rel);
7575
static void RelationRemoveIndexes(Relation relation);
7676
static void RelationRemoveInheritance(Relation relation);
77-
static void AddNewRelationType(char *typeName, Oid new_rel_oid,
78-
Oid new_type_oid);
77+
static void AddNewRelationType(const char *typeName,
78+
Oid typeNamespace,
79+
Oid new_rel_oid,
80+
Oid new_type_oid);
7981
static void StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin);
8082
static void StoreRelCheck(Relation rel, char *ccname, char *ccbin);
8183
static void StoreConstraints(Relation rel, TupleDesc tupdesc);
@@ -242,7 +244,7 @@ heap_create(char *relname,
242244
* have to take special care for those rels that should be nailed
243245
* in cache and/or are shared across databases.
244246
*/
245-
if (relname && relnamespace == PG_CATALOG_NAMESPACE)
247+
if (relnamespace == PG_CATALOG_NAMESPACE)
246248
{
247249
if (strcmp(TypeRelationName, relname) == 0)
248250
{
@@ -622,7 +624,10 @@ AddNewRelationTuple(Relation pg_class_desc,
622624
* --------------------------------
623625
*/
624626
static void
625-
AddNewRelationType(char *typeName, Oid new_rel_oid, Oid new_type_oid)
627+
AddNewRelationType(const char *typeName,
628+
Oid typeNamespace,
629+
Oid new_rel_oid,
630+
Oid new_type_oid)
626631
{
627632
/*
628633
* The sizes are set to oid size because it makes implementing sets
@@ -634,18 +639,19 @@ AddNewRelationType(char *typeName, Oid new_rel_oid, Oid new_type_oid)
634639
* true makes sets much easier, and it isn't used by anything else.
635640
*/
636641
TypeCreate(typeName, /* type name */
642+
typeNamespace, /* type namespace */
637643
new_type_oid, /* preassigned oid for type */
638644
new_rel_oid, /* relation oid */
639645
sizeof(Oid), /* internal size */
640646
-1, /* external size */
641-
'c', /* type-type (catalog) */
647+
'c', /* type-type (complex) */
642648
',', /* default array delimiter */
643-
"oidin", /* input procedure */
644-
"oidout", /* output procedure */
645-
"oidin", /* receive procedure */
646-
"oidout", /* send procedure */
647-
NULL, /* array element type - irrelevant */
648-
NULL, /* baseType Name -- typically for domains */
649+
F_OIDIN, /* input procedure */
650+
F_OIDOUT, /* output procedure */
651+
F_OIDIN, /* receive procedure */
652+
F_OIDOUT, /* send procedure */
653+
InvalidOid, /* array element type - irrelevant */
654+
InvalidOid, /* domain base type - irrelevant */
649655
NULL, /* default type value - none */
650656
NULL, /* default type binary representation */
651657
true, /* passed by value */
@@ -744,7 +750,7 @@ heap_create_with_catalog(char *relname,
744750
* NOTE: we could get a unique-index failure here, in case the same name
745751
* has already been used for a type.
746752
*/
747-
AddNewRelationType(relname, new_rel_oid, new_type_oid);
753+
AddNewRelationType(relname, relnamespace, new_rel_oid, new_type_oid);
748754

749755
/*
750756
* now add tuples to pg_attribute for the attributes in our new
@@ -1002,15 +1008,13 @@ RelationTruncateIndexes(Oid heapId)
10021008
*/
10031009

10041010
void
1005-
heap_truncate(const char *relname)
1011+
heap_truncate(Oid rid)
10061012
{
10071013
Relation rel;
1008-
Oid rid;
10091014

10101015
/* Open relation for processing, and grab exclusive access on it. */
10111016

1012-
rel = heap_openr(relname, AccessExclusiveLock);
1013-
rid = RelationGetRelid(rel);
1017+
rel = heap_open(rid, AccessExclusiveLock);
10141018

10151019
/*
10161020
* TRUNCATE TABLE within a transaction block is dangerous, because if
@@ -1217,21 +1221,22 @@ DeleteTypeTuple(Relation rel)
12171221
* ----------------------------------------------------------------
12181222
*/
12191223
void
1220-
heap_drop_with_catalog(const char *relname,
1224+
heap_drop_with_catalog(Oid rid,
12211225
bool allow_system_table_mods)
12221226
{
12231227
Relation rel;
1224-
Oid rid;
1228+
Oid toasttableOid;
12251229
bool has_toasttable;
1226-
bool istemp = is_temp_rel_name(relname);
1230+
bool istemp;
12271231
int i;
12281232

12291233
/*
12301234
* Open and lock the relation.
12311235
*/
1232-
rel = heap_openr(relname, AccessExclusiveLock);
1233-
rid = RelationGetRelid(rel);
1236+
rel = heap_open(rid, AccessExclusiveLock);
12341237
has_toasttable = rel->rd_rel->reltoastrelid != InvalidOid;
1238+
toasttableOid = rel->rd_rel->reltoastrelid;
1239+
istemp = is_temp_rel_name(RelationGetRelationName(rel));
12351240

12361241
/*
12371242
* prevent deletion of system relations
@@ -1319,12 +1324,7 @@ heap_drop_with_catalog(const char *relname,
13191324
remove_temp_rel_by_relid(rid);
13201325

13211326
if (has_toasttable)
1322-
{
1323-
char toast_relname[NAMEDATALEN];
1324-
1325-
sprintf(toast_relname, "pg_toast_%u", rid);
1326-
heap_drop_with_catalog(toast_relname, true);
1327-
}
1327+
heap_drop_with_catalog(toasttableOid, true);
13281328
}
13291329

13301330

0 commit comments

Comments
 (0)