88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.145 2000/09/29 18:21:25 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.146 2000/09/30 18:28:53 tgl Exp $
1212 *
1313 *
1414 * INTERFACE ROUTINES
@@ -1064,32 +1064,32 @@ DeleteRelationTuple(Relation rel)
10641064 * RelationTruncateIndexes - This routine is used to truncate all
10651065 * indices associated with the heap relation to zero tuples.
10661066 * The routine will truncate and then reconstruct the indices on
1067- * the relation specified by theheapRelation parameter.
1067+ * the relation specified by theheapId parameter.
10681068 * --------------------------------
10691069 */
10701070static void
1071- RelationTruncateIndexes (Relation heapRelation )
1071+ RelationTruncateIndexes (Oid heapId )
10721072{
1073- Relation indexRelation ,
1074- currentIndex ;
1073+ Relation indexRelation ;
10751074ScanKeyData entry ;
10761075HeapScanDesc scan ;
1077- HeapTuple indexTuple ,
1078- classTuple ;
1079- IndexInfo * indexInfo ;
1080- Oid heapId ,
1081- indexId ,
1082- accessMethodId ;
1083-
1084- heapId = RelationGetRelid (heapRelation );
1076+ HeapTuple indexTuple ;
10851077
1086- /* Scan pg_index to find indexes onheapRelation */
1078+ /* Scan pg_index to find indexes onspecified heap */
10871079indexRelation = heap_openr (IndexRelationName ,AccessShareLock );
10881080ScanKeyEntryInitialize (& entry ,0 ,Anum_pg_index_indrelid ,F_OIDEQ ,
10891081ObjectIdGetDatum (heapId ));
10901082scan = heap_beginscan (indexRelation , false,SnapshotNow ,1 ,& entry );
1083+
10911084while (HeapTupleIsValid (indexTuple = heap_getnext (scan ,0 )))
10921085{
1086+ Oid indexId ,
1087+ accessMethodId ;
1088+ IndexInfo * indexInfo ;
1089+ HeapTuple classTuple ;
1090+ Relation heapRelation ,
1091+ currentIndex ;
1092+
10931093/*
10941094 * For each index, fetch info needed for index_build
10951095 */
@@ -1105,10 +1105,16 @@ RelationTruncateIndexes(Relation heapRelation)
11051105indexId );
11061106accessMethodId = ((Form_pg_class )GETSTRUCT (classTuple ))-> relam ;
11071107
1108+ /*
1109+ * We have to re-open the heap rel each time through this loop
1110+ * because index_build will close it again. We need grab no lock,
1111+ * however, because we assume heap_truncate is holding an exclusive
1112+ * lock on the heap rel.
1113+ */
1114+ heapRelation = heap_open (heapId ,NoLock );
1115+
11081116/* Open the index relation */
11091117currentIndex = index_open (indexId );
1110- if (currentIndex == NULL )
1111- elog (ERROR ,"RelationTruncateIndexes: can't open index relation" );
11121118
11131119/* Obtain exclusive lock on it, just to be sure */
11141120LockRelation (currentIndex ,AccessExclusiveLock );
@@ -1127,15 +1133,10 @@ RelationTruncateIndexes(Relation heapRelation)
11271133InitIndexStrategy (indexInfo -> ii_NumIndexAttrs ,
11281134currentIndex ,accessMethodId );
11291135index_build (heapRelation ,currentIndex ,indexInfo ,NULL );
1130-
11311136/*
11321137 * index_build will close both the heap and index relations (but
1133- * not give up the locks we hold on them).That's fine for the
1134- * index, but we need to open the heap again. We need no new
1135- * lock, since this backend still has the exclusive lock grabbed
1136- * by heap_truncate.
1138+ * not give up the locks we hold on them).
11371139 */
1138- heapRelation = heap_open (heapId ,NoLock );
11391140}
11401141
11411142/* Complete the scan and close pg_index */
@@ -1191,17 +1192,12 @@ heap_truncate(char *relname)
11911192rel -> rd_nblocks = 0 ;
11921193
11931194/* If this relation has indexes, truncate the indexes too */
1194- RelationTruncateIndexes (rel );
1195+ RelationTruncateIndexes (rid );
11951196
11961197/*
11971198 * Close the relation, but keep exclusive lock on it until commit.
11981199 */
11991200heap_close (rel ,NoLock );
1200-
1201- /*
1202- * Is this really necessary?
1203- */
1204- RelationForgetRelation (rid );
12051201}
12061202
12071203