88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.136 2001/01/23 04:32:21 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.137 2001/01/24 00:06:07 tgl Exp $
1212 *
1313 *
1414 * INTERFACE ROUTINES
6262static Oid GetHeapRelationOid (char * heapRelationName ,char * indexRelationName ,
6363bool istemp );
6464static TupleDesc BuildFuncTupleDesc (Oid funcOid );
65- static TupleDesc ConstructTupleDescriptor (Oid heapoid , Relation heapRelation ,
65+ static TupleDesc ConstructTupleDescriptor (Relation heapRelation ,
6666int numatts ,AttrNumber * attNums );
6767static void ConstructIndexReldesc (Relation indexRelation ,Oid amoid );
6868static Oid UpdateRelationRelation (Relation indexRelation ,char * temp_relname );
@@ -166,7 +166,7 @@ BuildFuncTupleDesc(Oid funcOid)
166166Oid retType ;
167167
168168/*
169- * Allocate and zero a tuple descriptor.
169+ * Allocate and zero a tuple descriptor for a one-column tuple .
170170 */
171171funcTupDesc = CreateTemplateTupleDesc (1 );
172172funcTupDesc -> attrs [0 ]= (Form_pg_attribute )palloc (ATTRIBUTE_TUPLE_SIZE );
@@ -208,7 +208,7 @@ BuildFuncTupleDesc(Oid funcOid)
208208funcTupDesc -> attrs [0 ]-> attbyval = ((Form_pg_type )GETSTRUCT (tuple ))-> typbyval ;
209209funcTupDesc -> attrs [0 ]-> attcacheoff = -1 ;
210210funcTupDesc -> attrs [0 ]-> atttypmod = -1 ;
211- funcTupDesc -> attrs [0 ]-> attstorage = 'p' ;
211+ funcTupDesc -> attrs [0 ]-> attstorage = (( Form_pg_type ) GETSTRUCT ( tuple )) -> typstorage ;
212212funcTupDesc -> attrs [0 ]-> attalign = ((Form_pg_type )GETSTRUCT (tuple ))-> typalign ;
213213
214214ReleaseSysCache (tuple );
@@ -223,8 +223,7 @@ BuildFuncTupleDesc(Oid funcOid)
223223 * ----------------------------------------------------------------
224224 */
225225static TupleDesc
226- ConstructTupleDescriptor (Oid heapoid ,
227- Relation heapRelation ,
226+ ConstructTupleDescriptor (Relation heapRelation ,
228227int numatts ,
229228AttrNumber * attNums )
230229{
@@ -253,25 +252,16 @@ ConstructTupleDescriptor(Oid heapoid,
253252{
254253AttrNumber atnum ;/* attributeNumber[attributeOffset] */
255254AttrNumber atind ;
256- char * from ;/* used to simplify memcpy below */
257- char * to ;/* used to simplify memcpy below */
255+ Form_pg_attribute from ;
256+ Form_pg_attribute to ;
258257
259258/* ----------------
260- * get the attribute number and make sure it's valid
259+ * get the attribute number and make sure it's valid;
260+ * determine which attribute descriptor to copy
261261 * ----------------
262262 */
263263atnum = attNums [i ];
264- if (atnum > natts )
265- elog (ERROR ,"Cannot create index: attribute %d does not exist" ,
266- atnum );
267264
268- indexTupDesc -> attrs [i ]=
269- (Form_pg_attribute )palloc (ATTRIBUTE_TUPLE_SIZE );
270-
271- /* ----------------
272- * determine which tuple descriptor to copy
273- * ----------------
274- */
275265if (!AttrNumberIsForUserDefinedAttr (atnum ))
276266{
277267/* ----------------
@@ -285,44 +275,47 @@ ConstructTupleDescriptor(Oid heapoid,
285275elog (ERROR ,"Cannot create index on system attribute: attribute number out of range (%d)" ,atnum );
286276atind = (- atnum )- 1 ;
287277
288- from = ( char * ) ( & sysatts [atind ]) ;
278+ from = & sysatts [atind ];
289279}
290280else
291281{
292282/* ----------------
293283 * here we are indexing on a normal attribute (1...n)
294284 * ----------------
295285 */
286+ if (atnum > natts )
287+ elog (ERROR ,"Cannot create index: attribute %d does not exist" ,
288+ atnum );
296289atind = AttrNumberGetAttrOffset (atnum );
297290
298- from = ( char * ) ( heapTupDesc -> attrs [atind ]) ;
291+ from = heapTupDesc -> attrs [atind ];
299292}
300293
301294/* ----------------
302295 * now that we've determined the "from", let's copy
303296 * the tuple desc data...
304297 * ----------------
305298 */
306- to = (char * ) (indexTupDesc -> attrs [i ]);
299+ indexTupDesc -> attrs [i ]= to =
300+ (Form_pg_attribute )palloc (ATTRIBUTE_TUPLE_SIZE );
307301memcpy (to ,from ,ATTRIBUTE_TUPLE_SIZE );
308302
309303/*
310304 * Fix the stuff that should not be the same as the underlying attr
311305 */
312- (( Form_pg_attribute ) to ) -> attnum = i + 1 ;
306+ to -> attnum = i + 1 ;
313307
314- (( Form_pg_attribute ) to ) -> attdispersion = 0.0 ;
315- (( Form_pg_attribute ) to ) -> attnotnull = false;
316- (( Form_pg_attribute ) to ) -> atthasdef = false;
317- (( Form_pg_attribute ) to ) -> attcacheoff = -1 ;
308+ to -> attdispersion = 0.0 ;
309+ to -> attnotnull = false;
310+ to -> atthasdef = false;
311+ to -> attcacheoff = -1 ;
318312
319- /* ----------------
320- * now we have to drop in the proper relation descriptor
321- * into the copied tuple form's attrelid and we should be
322- * all set.
323- * ----------------
313+ /*
314+ * We do not yet have the correct relation OID for the index,
315+ * so just set it invalid for now. InitializeAttributeOids()
316+ * will fix it later.
324317 */
325- (( Form_pg_attribute ) to ) -> attrelid = heapoid ;
318+ to -> attrelid = InvalidOid ;
326319}
327320
328321return indexTupDesc ;
@@ -916,8 +909,7 @@ index_create(char *heapRelationName,
916909if (OidIsValid (indexInfo -> ii_FuncOid ))
917910indexTupDesc = BuildFuncTupleDesc (indexInfo -> ii_FuncOid );
918911else
919- indexTupDesc = ConstructTupleDescriptor (heapoid ,
920- heapRelation ,
912+ indexTupDesc = ConstructTupleDescriptor (heapRelation ,
921913indexInfo -> ii_NumKeyAttrs ,
922914indexInfo -> ii_KeyAttrNumbers );
923915