8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.128 2000/05/25 21:25:32 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.128.2.1 2000/09/30 18:47:07 tgl Exp $
12
12
*
13
13
*
14
14
* INTERFACE ROUTINES
@@ -1091,44 +1091,42 @@ DeleteRelationTuple(Relation rel)
1091
1091
* RelationTruncateIndexes - This routine is used to truncate all
1092
1092
* indices associated with the heap relation to zero tuples.
1093
1093
* The routine will truncate and then reconstruct the indices on
1094
- * the relation specified by the heapRelation parameter.
1094
+ * the relation specified by the heapId parameter.
1095
1095
* --------------------------------
1096
1096
*/
1097
1097
static void
1098
- RelationTruncateIndexes (Relation heapRelation )
1098
+ RelationTruncateIndexes (Oid heapId )
1099
1099
{
1100
- Relation indexRelation ,
1101
- currentIndex ;
1100
+ Relation indexRelation ;
1102
1101
ScanKeyData entry ;
1103
1102
HeapScanDesc scan ;
1104
- HeapTuple indexTuple ,
1105
- procTuple ,
1106
- classTuple ;
1107
- Form_pg_index index ;
1108
- Oid heapId ,
1109
- indexId ,
1110
- procId ,
1111
- accessMethodId ;
1112
- Node * oldPred = NULL ;
1113
- PredInfo * predInfo ;
1114
- List * cnfPred = NULL ;
1115
- AttrNumber * attributeNumberA ;
1116
- FuncIndexInfo fInfo ,
1117
- * funcInfo = NULL ;
1118
- int i ,
1119
- numberOfAttributes ;
1120
- char * predString ;
1121
-
1122
- heapId = RelationGetRelid (heapRelation );
1123
-
1124
- /* Scan pg_index to find indexes on heapRelation */
1103
+ HeapTuple indexTuple ;
1125
1104
1105
+ /* Scan pg_index to find indexes on specified heap */
1126
1106
indexRelation = heap_openr (IndexRelationName , AccessShareLock );
1127
1107
ScanKeyEntryInitialize (& entry , 0 , Anum_pg_index_indrelid , F_OIDEQ ,
1128
1108
ObjectIdGetDatum (heapId ));
1129
1109
scan = heap_beginscan (indexRelation , false, SnapshotNow , 1 , & entry );
1110
+
1130
1111
while (HeapTupleIsValid (indexTuple = heap_getnext (scan , 0 )))
1131
1112
{
1113
+ Relation heapRelation ,
1114
+ currentIndex ;
1115
+ HeapTuple procTuple ,
1116
+ classTuple ;
1117
+ Form_pg_index index ;
1118
+ Oid indexId ,
1119
+ procId ,
1120
+ accessMethodId ;
1121
+ Node * oldPred = NULL ;
1122
+ PredInfo * predInfo ;
1123
+ List * cnfPred = NULL ;
1124
+ AttrNumber * attributeNumberA ;
1125
+ FuncIndexInfo fInfo ,
1126
+ * funcInfo = NULL ;
1127
+ int i ,
1128
+ numberOfAttributes ;
1129
+ char * predString ;
1132
1130
1133
1131
/*
1134
1132
* For each index, fetch index attributes so we can apply
@@ -1183,10 +1181,17 @@ RelationTruncateIndexes(Relation heapRelation)
1183
1181
elog (ERROR , "RelationTruncateIndexes: index access method not found" );
1184
1182
accessMethodId = ((Form_pg_class ) GETSTRUCT (classTuple ))-> relam ;
1185
1183
1184
+ /*
1185
+ * We have to re-open the heap rel each time through this loop
1186
+ * because index_build will close it again. We need grab no lock,
1187
+ * however, because we assume heap_truncate is holding an exclusive
1188
+ * lock on the heap rel.
1189
+ */
1190
+ heapRelation = heap_open (heapId , NoLock );
1191
+ Assert (heapRelation != NULL );
1192
+
1186
1193
/* Open our index relation */
1187
1194
currentIndex = index_open (indexId );
1188
- if (currentIndex == NULL )
1189
- elog (ERROR , "RelationTruncateIndexes: can't open index relation" );
1190
1195
1191
1196
/* Obtain exclusive lock on it, just to be sure */
1192
1197
LockRelation (currentIndex , AccessExclusiveLock );
@@ -1205,16 +1210,10 @@ RelationTruncateIndexes(Relation heapRelation)
1205
1210
InitIndexStrategy (numberOfAttributes , currentIndex , accessMethodId );
1206
1211
index_build (heapRelation , currentIndex , numberOfAttributes ,
1207
1212
attributeNumberA , 0 , NULL , funcInfo , predInfo );
1208
-
1209
1213
/*
1210
1214
* index_build will close both the heap and index relations (but
1211
- * not give up the locks we hold on them). That's fine for the
1212
- * index, but we need to open the heap again. We need no new
1213
- * lock, since this backend still has the exclusive lock grabbed
1214
- * by heap_truncate.
1215
+ * not give up the locks we hold on them).
1215
1216
*/
1216
- heapRelation = heap_open (heapId , NoLock );
1217
- Assert (heapRelation != NULL );
1218
1217
}
1219
1218
1220
1219
/* Complete the scan and close pg_index */
@@ -1270,17 +1269,12 @@ heap_truncate(char *relname)
1270
1269
rel -> rd_nblocks = 0 ;
1271
1270
1272
1271
/* If this relation has indexes, truncate the indexes too */
1273
- RelationTruncateIndexes (rel );
1272
+ RelationTruncateIndexes (rid );
1274
1273
1275
1274
/*
1276
1275
* Close the relation, but keep exclusive lock on it until commit.
1277
1276
*/
1278
1277
heap_close (rel , NoLock );
1279
-
1280
- /*
1281
- * Is this really necessary?
1282
- */
1283
- RelationForgetRelation (rid );
1284
1278
}
1285
1279
1286
1280
0 commit comments