8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*
14
14
* INTERFACE ROUTINES
68
68
69
69
70
70
static 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 );
74
73
static void DeleteAttributeTuples (Relation rel );
75
74
static void DeleteRelationTuple (Relation rel );
76
75
static void DeleteTypeTuple (Relation rel );
77
76
static void RelationRemoveIndexes (Relation relation );
78
77
static 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 );
80
80
static void StoreAttrDefault (Relation rel , AttrNumber attnum , char * adbin ,
81
81
bool updatePgAttribute );
82
82
static void StoreRelCheck (Relation rel , char * ccname , char * ccbin );
@@ -317,6 +317,7 @@ heap_create(char *relname,
317
317
strcpy (RelationGetPhysicalRelationName (rel ), relname );
318
318
rel -> rd_rel -> relkind = RELKIND_UNCATALOGED ;
319
319
rel -> rd_rel -> relnatts = natts ;
320
+ rel -> rd_rel -> reltype = InvalidOid ;
320
321
if (tupDesc -> constr )
321
322
rel -> rd_rel -> relchecks = tupDesc -> constr -> num_check ;
322
323
@@ -325,12 +326,6 @@ heap_create(char *relname,
325
326
326
327
RelationGetRelid (rel ) = relid ;
327
328
328
- if (nailme )
329
- {
330
- /* for system relations, set the reltype field here */
331
- rel -> rd_rel -> reltype = relid ;
332
- }
333
-
334
329
rel -> rd_node .tblNode = tblNode ;
335
330
rel -> rd_node .relNode = relid ;
336
331
rel -> rd_rel -> relfilenode = relid ;
@@ -373,18 +368,17 @@ heap_storage_create(Relation rel)
373
368
* performs a scan to ensure that no relation with the
374
369
* same name already exists.
375
370
*
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.
378
372
*
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
380
377
* to the new relation.
381
378
*
382
- * 5 ) AddNewAttributeTuples() is called to register the
379
+ * 6 ) AddNewAttributeTuples() is called to register the
383
380
* new relation's schema in pg_attribute.
384
381
*
385
- * 6) AddNewRelationTuple() is called to register the
386
- * relation itself in the catalogs.
387
- *
388
382
* 7) StoreConstraints is called () - vadim 08/22/97
389
383
*
390
384
* 8) the relations are closed and the new relation's oid
@@ -656,6 +650,7 @@ static void
656
650
AddNewRelationTuple (Relation pg_class_desc ,
657
651
Relation new_rel_desc ,
658
652
Oid new_rel_oid ,
653
+ Oid new_type_oid ,
659
654
int natts ,
660
655
char relkind ,
661
656
char * temp_relname )
@@ -665,7 +660,7 @@ AddNewRelationTuple(Relation pg_class_desc,
665
660
Relation idescs [Num_pg_class_indices ];
666
661
667
662
/* ----------------
668
- * first we munge some of the information in our
663
+ * first we update some of the information in our
669
664
* uncataloged relation's relation descriptor.
670
665
* ----------------
671
666
*/
@@ -694,6 +689,7 @@ AddNewRelationTuple(Relation pg_class_desc,
694
689
new_rel_reltup -> reltuples = 1000 ;
695
690
696
691
new_rel_reltup -> relowner = GetUserId ();
692
+ new_rel_reltup -> reltype = new_type_oid ;
697
693
new_rel_reltup -> relkind = relkind ;
698
694
new_rel_reltup -> relnatts = natts ;
699
695
@@ -705,6 +701,8 @@ AddNewRelationTuple(Relation pg_class_desc,
705
701
tup = heap_addheader (Natts_pg_class_fixed ,
706
702
CLASS_TUPLE_SIZE ,
707
703
(char * ) new_rel_reltup );
704
+
705
+ /* force tuple to have the desired OID */
708
706
tup -> t_data -> t_oid = new_rel_oid ;
709
707
710
708
/*
@@ -738,10 +736,8 @@ AddNewRelationTuple(Relation pg_class_desc,
738
736
* --------------------------------
739
737
*/
740
738
static void
741
- AddNewRelationType (char * typeName , Oid new_rel_oid )
739
+ AddNewRelationType (char * typeName , Oid new_rel_oid , Oid new_type_oid )
742
740
{
743
- Oid new_type_oid ;
744
-
745
741
/*
746
742
* The sizes are set to oid size because it makes implementing sets
747
743
* MUCH easier, and no one (we hope) uses these fields to figure out
@@ -750,23 +746,25 @@ AddNewRelationType(char *typeName, Oid new_rel_oid)
750
746
* actually get is the oid of a tuple in the pg_proc catalog, so the
751
747
* size of the "set" is the size of an oid. Similarly, byval being
752
748
* 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 */
770
768
}
771
769
772
770
/* --------------------------------
@@ -785,6 +783,7 @@ heap_create_with_catalog(char *relname,
785
783
Relation pg_class_desc ;
786
784
Relation new_rel_desc ;
787
785
Oid new_rel_oid ;
786
+ Oid new_type_oid ;
788
787
int natts = tupdesc -> natts ;
789
788
char * temp_relname = NULL ;
790
789
@@ -814,18 +813,10 @@ heap_create_with_catalog(char *relname,
814
813
}
815
814
816
815
/* ----------------
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.)
829
820
*
830
821
* Note: The call to heap_create() changes relname for
831
822
* temp tables; it becomes the true physical relname.
@@ -836,35 +827,47 @@ heap_create_with_catalog(char *relname,
836
827
new_rel_desc = heap_create (relname , tupdesc , istemp , false,
837
828
allow_system_table_mods );
838
829
830
+ /* Fetch the relation OID assigned by heap_create */
839
831
new_rel_oid = new_rel_desc -> rd_att -> attrs [0 ]-> attrelid ;
840
832
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 ();
847
835
848
836
/* ----------------
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.
857
842
* ----------------
858
843
*/
859
844
pg_class_desc = heap_openr (RelationRelationName , RowExclusiveLock );
860
845
861
846
AddNewRelationTuple (pg_class_desc ,
862
847
new_rel_desc ,
863
848
new_rel_oid ,
849
+ new_type_oid ,
864
850
natts ,
865
851
relkind ,
866
852
temp_relname );
867
853
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
+
868
871
StoreConstraints (new_rel_desc );
869
872
870
873
if (istemp )
@@ -912,7 +915,6 @@ heap_create_with_catalog(char *relname,
912
915
* attribute catalog (needed?). (Anything else?)
913
916
*
914
917
* get proper relation from relation catalog (if not arg)
915
- * check if relation is vital (strcmp()/reltype?)
916
918
* scan attribute catalog deleting attributes of reldesc
917
919
* (necessary?)
918
920
* delete relation from relation catalog
0 commit comments