Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36786a8

Browse files
committed
Fix nasty TRUNCATE bug reported by Darrin Ladd. RelationTruncateIndexes
would close and then re-open rel being truncated. Depending on theluck of the draw, the re-opened relcache entry might or might not beat the same physical location as before. Unfortunately, if it wasn'tthen heap_truncate would crash and burn, because it still had a pointerat the old location. Fix is to open and then close rel inRelationTruncateIndexes, so that rel's refcount never goes to zerountil heap_truncate is done.
1 parent0ba77c1 commit36786a8

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

‎src/backend/catalog/heap.c

Lines changed: 24 additions & 28 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.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
*/
10701070
staticvoid
1071-
RelationTruncateIndexes(RelationheapRelation)
1071+
RelationTruncateIndexes(OidheapId)
10721072
{
1073-
RelationindexRelation,
1074-
currentIndex;
1073+
RelationindexRelation;
10751074
ScanKeyDataentry;
10761075
HeapScanDescscan;
1077-
HeapTupleindexTuple,
1078-
classTuple;
1079-
IndexInfo*indexInfo;
1080-
OidheapId,
1081-
indexId,
1082-
accessMethodId;
1083-
1084-
heapId=RelationGetRelid(heapRelation);
1076+
HeapTupleindexTuple;
10851077

1086-
/* Scan pg_index to find indexes onheapRelation */
1078+
/* Scan pg_index to find indexes onspecified heap */
10871079
indexRelation=heap_openr(IndexRelationName,AccessShareLock);
10881080
ScanKeyEntryInitialize(&entry,0,Anum_pg_index_indrelid,F_OIDEQ,
10891081
ObjectIdGetDatum(heapId));
10901082
scan=heap_beginscan(indexRelation, false,SnapshotNow,1,&entry);
1083+
10911084
while (HeapTupleIsValid(indexTuple=heap_getnext(scan,0)))
10921085
{
1086+
OidindexId,
1087+
accessMethodId;
1088+
IndexInfo*indexInfo;
1089+
HeapTupleclassTuple;
1090+
RelationheapRelation,
1091+
currentIndex;
1092+
10931093
/*
10941094
* For each index, fetch info needed for index_build
10951095
*/
@@ -1105,10 +1105,16 @@ RelationTruncateIndexes(Relation heapRelation)
11051105
indexId);
11061106
accessMethodId= ((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 */
11091117
currentIndex=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 */
11141120
LockRelation(currentIndex,AccessExclusiveLock);
@@ -1127,15 +1133,10 @@ RelationTruncateIndexes(Relation heapRelation)
11271133
InitIndexStrategy(indexInfo->ii_NumIndexAttrs,
11281134
currentIndex,accessMethodId);
11291135
index_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)
11911192
rel->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
*/
11991200
heap_close(rel,NoLock);
1200-
1201-
/*
1202-
* Is this really necessary?
1203-
*/
1204-
RelationForgetRelation(rid);
12051201
}
12061202

12071203

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp