8000 Back-patch fix for TRUNCATE failure on relations with indexes. · percona/postgres@31ab083 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31ab083

Browse files
committed
Back-patch fix for TRUNCATE failure on relations with indexes.
1 parent 956ba75 commit 31ab083

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

src/backend/catalog/heap.c

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* 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 $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1091,44 +1091,42 @@ DeleteRelationTuple(Relation rel)
10911091
* RelationTruncateIndexes - This routine is used to truncate all
10921092
* indices associated with the heap relation to zero tuples.
10931093
* 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.
10951095
* --------------------------------
10961096
*/
10971097
static void
1098-
RelationTruncateIndexes(Relation heapRelation)
1098+
RelationTruncateIndexes(Oid heapId)
10991099
{
1100-
Relation indexRelation,
1101-
currentIndex;
1100+
Relation indexRelation;
11021101
ScanKeyData entry;
11031102
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;
11251104

1105+
/* Scan pg_index to find indexes on specified heap */
11261106
indexRelation = heap_openr(IndexRelationName, AccessShareLock);
11271107
ScanKeyEntryInitialize(&entry, 0, Anum_pg_index_indrelid, F_OIDEQ,
11281108
ObjectIdGetDatum(heapId));
11291109
scan = heap_beginscan(indexRelation, false, SnapshotNow, 1, &entry);
1110+
11301111
while (HeapTupleIsValid(indexTuple = heap_getnext(scan, 0)))
11311112
{
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;
11321130

11331131
/*
11341132
* For each index, fetch index attributes so we can apply
@@ -1183,10 +1181,17 @@ RelationTruncateIndexes(Relation heapRelation)
11831181
elog(ERROR, "RelationTruncateIndexes: index access method not found");
11841182
accessMethodId = ((Form_pg_class) GETSTRUCT(classTuple))->relam;
11851183

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+
11861193
/* Open our index relation */
11871194
currentIndex = index_open(indexId);
1188-
if (currentIndex == NULL)
1189-
elog(ERROR, "RelationTruncateIndexes: can't open index relation");
11901195

11911196
/* Obtain exclusive lock on it, just to be sure */
11921197
LockRelation(currentIndex, AccessExclusiveLock);
@@ -1205,16 +1210,10 @@ RelationTruncateIndexes(Relation heapRelation)
12051210
InitIndexStrategy(numberOfAttributes, currentIndex, accessMethodId);
12061211
index_build(heapRelation, currentIndex, numberOfAttributes,
12071212
attributeNumberA, 0, NULL, funcInfo, predInfo);
1208-
12091213
/*
12101214
* 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).
12151216
*/
1216-
heapRelation = heap_open(heapId, NoLock);
1217-
Assert(heapRelation != NULL);
12181217
}
12191218

12201219
/* Complete the scan and close pg_index */
@@ -1270,17 +1269,12 @@ heap_truncate(char *relname)
12701269
rel->rd_nblocks = 0;
12711270

12721271
/* If this relation has indexes, truncate the indexes too */
1273-
RelationTruncateIndexes(rel);
1272+
RelationTruncateIndexes(rid);
12741273

12751274
/*
12761275
* Close the relation, but keep exclusive lock on it until commit.
12771276
*/
12781277
heap_close(rel, NoLock);
1279-
1280-
/*
1281-
* Is this really necessary?
1282-
*/
1283-
RelationForgetRelation(rid);
12841278
}
12851279

12861280

0 commit comments

Comments
 (0)
0