88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.158 2001/01/24 19:42:51 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.159 2001/02/12 20:07:21 tgl Exp $
1212 *
1313 *
1414 * INTERFACE ROUTINES
6868
6969
7070static void AddNewRelationTuple (Relation pg_class_desc ,
71- Relation new_rel_desc ,Oid new_rel_oid ,
72- int natts ,
73- char relkind ,char * temp_relname );
71+ Relation new_rel_desc ,Oid new_rel_oid ,Oid new_type_oid ,
72+ int natts ,char relkind ,char * temp_relname );
7473static void DeleteAttributeTuples (Relation rel );
7574static void DeleteRelationTuple (Relation rel );
7675static void DeleteTypeTuple (Relation rel );
7776static void RelationRemoveIndexes (Relation relation );
7877static void RelationRemoveInheritance (Relation relation );
79- static void AddNewRelationType (char * typeName ,Oid new_rel_oid );
78+ static void AddNewRelationType (char * typeName ,Oid new_rel_oid ,
79+ Oid new_type_oid );
8080static void StoreAttrDefault (Relation rel ,AttrNumber attnum ,char * adbin ,
8181bool updatePgAttribute );
8282static void StoreRelCheck (Relation rel ,char * ccname ,char * ccbin );
@@ -317,6 +317,7 @@ heap_create(char *relname,
317317strcpy (RelationGetPhysicalRelationName (rel ),relname );
318318rel -> rd_rel -> relkind = RELKIND_UNCATALOGED ;
319319rel -> rd_rel -> relnatts = natts ;
320+ rel -> rd_rel -> reltype = InvalidOid ;
320321if (tupDesc -> constr )
321322rel -> rd_rel -> relchecks = tupDesc -> constr -> num_check ;
322323
@@ -325,12 +326,6 @@ heap_create(char *relname,
325326
326327RelationGetRelid (rel )= relid ;
327328
328- if (nailme )
329- {
330- /* for system relations, set the reltype field here */
331- rel -> rd_rel -> reltype = relid ;
332- }
333-
334329rel -> rd_node .tblNode = tblNode ;
335330rel -> rd_node .relNode = relid ;
336331rel -> rd_rel -> relfilenode = relid ;
@@ -373,18 +368,17 @@ heap_storage_create(Relation rel)
373368 * performs a scan to ensure that no relation with the
374369 * same name already exists.
375370 *
376- *3) heap_create_with_catalog() is called to create the new relation
377- * on disk.
371+ *3) heap_create() is called to create the new relation on disk.
378372 *
379- *4) TypeDefine() is called to define a new type corresponding
373+ *4) AddNewRelationTuple() is called to register the
374+ * relation in pg_class.
375+ *
376+ *5) TypeCreate() is called to define a new type corresponding
380377 * to the new relation.
381378 *
382- *5 ) AddNewAttributeTuples() is called to register the
379+ *6 ) AddNewAttributeTuples() is called to register the
383380 * new relation's schema in pg_attribute.
384381 *
385- *6) AddNewRelationTuple() is called to register the
386- * relation itself in the catalogs.
387- *
388382 *7) StoreConstraints is called ()- vadim 08/22/97
389383 *
390384 *8) the relations are closed and the new relation's oid
@@ -656,6 +650,7 @@ static void
656650AddNewRelationTuple (Relation pg_class_desc ,
657651Relation new_rel_desc ,
658652Oid new_rel_oid ,
653+ Oid new_type_oid ,
659654int natts ,
660655char relkind ,
661656char * temp_relname )
@@ -665,7 +660,7 @@ AddNewRelationTuple(Relation pg_class_desc,
665660Relation idescs [Num_pg_class_indices ];
666661
667662/* ----------------
668- *first wemunge some of the information in our
663+ *first weupdate some of the information in our
669664 *uncataloged relation's relation descriptor.
670665 * ----------------
671666 */
@@ -694,6 +689,7 @@ AddNewRelationTuple(Relation pg_class_desc,
694689new_rel_reltup -> reltuples = 1000 ;
695690
696691new_rel_reltup -> relowner = GetUserId ();
692+ new_rel_reltup -> reltype = new_type_oid ;
697693new_rel_reltup -> relkind = relkind ;
698694new_rel_reltup -> relnatts = natts ;
699695
@@ -705,6 +701,8 @@ AddNewRelationTuple(Relation pg_class_desc,
705701tup = heap_addheader (Natts_pg_class_fixed ,
706702CLASS_TUPLE_SIZE ,
707703 (char * )new_rel_reltup );
704+
705+ /* force tuple to have the desired OID */
708706tup -> t_data -> t_oid = new_rel_oid ;
709707
710708/*
@@ -738,10 +736,8 @@ AddNewRelationTuple(Relation pg_class_desc,
738736 * --------------------------------
739737 */
740738static void
741- AddNewRelationType (char * typeName ,Oid new_rel_oid )
739+ AddNewRelationType (char * typeName ,Oid new_rel_oid , Oid new_type_oid )
742740{
743- Oid new_type_oid ;
744-
745741/*
746742 * The sizes are set to oid size because it makes implementing sets
747743 * MUCH easier, and no one (we hope) uses these fields to figure out
@@ -750,23 +746,25 @@ AddNewRelationType(char *typeName, Oid new_rel_oid)
750746 * actually get is the oid of a tuple in the pg_proc catalog, so the
751747 * size of the "set" is the size of an oid. Similarly, byval being
752748 * true makes sets much easier, and it isn't used by anything else.
753- * Note the assumption that OIDs are the same size as int4s.
754- */
755- new_type_oid = TypeCreate (typeName ,/* type name */
756- new_rel_oid ,/* relation oid */
757- sizeof (Oid ),/* internal size */
758- sizeof (Oid ),/* external size */
759- 'c' ,/* type-type (catalog) */
760- ',' ,/* default array delimiter */
761- "int4in" ,/* input procedure */
762- "int4out" ,/* output procedure */
763- "int4in" ,/* receive procedure */
764- "int4out" ,/* send procedure */
765- NULL ,/* array element type - irrelevent */
766- "-" ,/* default type value */
767- (bool )1 ,/* passed by value */
768- 'i' ,/* default alignment */
769- 'p' );/* Not TOASTable */
749+ *
750+ * XXX Note the assumption that OIDs are the same size as int4s.
751+ */
752+ TypeCreate (typeName ,/* type name */
753+ new_type_oid ,/* preassigned oid for type */
754+ new_rel_oid ,/* relation oid */
755+ sizeof (Oid ),/* internal size */
756+ sizeof (Oid ),/* external size */
757+ 'c' ,/* type-type (catalog) */
758+ ',' ,/* default array delimiter */
759+ "int4in" ,/* input procedure */
760+ "int4out" ,/* output procedure */
761+ "int4in" ,/* receive procedure */
762+ "int4out" ,/* send procedure */
763+ NULL ,/* array element type - irrelevant */
764+ "-" ,/* default type value */
765+ true,/* passed by value */
766+ 'i' ,/* default alignment */
767+ 'p' );/* Not TOASTable */
770768}
771769
772770/* --------------------------------
@@ -785,6 +783,7 @@ heap_create_with_catalog(char *relname,
785783Relation pg_class_desc ;
786784Relation new_rel_desc ;
787785Oid new_rel_oid ;
786+ Oid new_type_oid ;
788787int natts = tupdesc -> natts ;
789788char * temp_relname = NULL ;
790789
@@ -814,18 +813,10 @@ heap_create_with_catalog(char *relname,
814813}
815814
816815/* ----------------
817- *RelnameFindRelid couldn't detect simultaneous
818- *creation. Uniqueness will be really checked by unique
819- *indexes of system tables but we couldn't check it here.
820- *We have to postpone creating the disk file for this
821- *relation.
822- *Another boolean parameter "storage_create" was added
823- *to heap_create() function. If the parameter is false
824- *heap_create() only registers an uncataloged relation
825- *to relation cache and heap_storage_create() should be
826- *called later.
827- *We could pull its relation oid from the newly formed
828- *relation descriptor.
816+ *Tell heap_create not to create a physical file; we'll do that
817+ *below after all our catalog updates are done. (This isn't really
818+ *necessary anymore, but we may as well avoid the cycles of creating
819+ *and deleting the file in case we fail.)
829820 *
830821 *Note: The call to heap_create() changes relname for
831822 *temp tables; it becomes the true physical relname.
@@ -836,35 +827,47 @@ heap_create_with_catalog(char *relname,
836827new_rel_desc = heap_create (relname ,tupdesc ,istemp , false,
837828allow_system_table_mods );
838829
830+ /* Fetch the relation OID assigned by heap_create */
839831new_rel_oid = new_rel_desc -> rd_att -> attrs [0 ]-> attrelid ;
840832
841- /* ----------------
842- *since defining a relation also defines a complex type,
843- *we add a new system type corresponding to the new relation.
844- * ----------------
845- */
846- AddNewRelationType (relname ,new_rel_oid );
833+ /* Assign an OID for the relation's tuple type */
834+ new_type_oid = newoid ();
847835
848836/* ----------------
849- *now add tuples to pg_attribute for the attributes in
850- *our new relation.
851- * ----------------
852- */
853- AddNewAttributeTuples (new_rel_oid ,tupdesc );
854-
855- /* ----------------
856- *now update the information in pg_class.
837+ *now create an entry in pg_class for the relation.
838+ *
839+ *NOTE: we could get a unique-index failure here, in case someone else
840+ *is creating the same relation name in parallel but hadn't committed
841+ *yet when we checked for a duplicate name above.
857842 * ----------------
858843 */
859844pg_class_desc = heap_openr (RelationRelationName ,RowExclusiveLock );
860845
861846AddNewRelationTuple (pg_class_desc ,
862847new_rel_desc ,
863848new_rel_oid ,
849+ new_type_oid ,
864850natts ,
865851relkind ,
866852temp_relname );
867853
854+ /* ----------------
855+ *since defining a relation also defines a complex type,
856+ *we add a new system type corresponding to the new relation.
857+ *
858+ *NOTE: we could get a unique-index failure here, in case the same name
859+ *has already been used for a type.
860+ * ----------------
861+ */
862+ AddNewRelationType (relname ,new_rel_oid ,new_type_oid );
863+
864+ /* ----------------
865+ *now add tuples to pg_attribute for the attributes in
866+ *our new relation.
867+ * ----------------
868+ */
869+ AddNewAttributeTuples (new_rel_oid ,tupdesc );
870+
868871StoreConstraints (new_rel_desc );
869872
870873if (istemp )
@@ -912,7 +915,6 @@ heap_create_with_catalog(char *relname,
912915 *attribute catalog (needed?). (Anything else?)
913916 *
914917 *get proper relation from relation catalog (if not arg)
915- *check if relation is vital (strcmp()/reltype?)
916918 *scan attribute catalog deleting attributes of reldesc
917919 *(necessary?)
918920 *delete relation from relation catalog