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

Commit37c652f

Browse files
committed
Fix CLUSTER ... or at least undo the bit-rot it's suffered since 6.5.
It's still pretty fundamentally bogus :-(.Freebie side benefit: ALTER TABLE RENAME works on indexes now.
1 parente10c559 commit37c652f

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

‎src/backend/commands/cluster.c

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.51 2000/04/12 17:14:57 momjian Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.52 2000/05/11 03:54:17 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -27,7 +27,6 @@
2727
#include"catalog/heap.h"
2828
#include"catalog/index.h"
2929
#include"catalog/pg_proc.h"
30-
#include"catalog/pg_type.h"
3130
#include"commands/cluster.h"
3231
#include"commands/rename.h"
3332
#include"optimizer/internal.h"
@@ -80,26 +79,13 @@ cluster(char *oldrelname, char *oldindexname)
8079
charsaveoldrelname[NAMEDATALEN];
8180
charsaveoldindexname[NAMEDATALEN];
8281

83-
8482
/*
85-
*Save theold namesbecause theywill get lost when the old
86-
*relations are destroyed.
83+
*Copy thearguments into local storage,because theyare probably
84+
*in palloc'd storage that will go away when we commit a transaction.
8785
*/
8886
strcpy(saveoldrelname,oldrelname);
8987
strcpy(saveoldindexname,oldindexname);
9088

91-
/*
92-
* I'm going to force all checking back into the commands.c function.
93-
*
94-
* Get the list if indicies for this relation. If the index we want is
95-
* among them, do not add it to the 'kill' list, as it will be handled
96-
* by the 'clean up' code which commits this transaction.
97-
*
98-
* I'm not using the SysCache, because this will happen but once, and the
99-
* slow way is the sure way in this case.
100-
*
101-
*/
102-
10389
/*
10490
* Like vacuum, cluster spans transactions, so I'm going to handle it
10591
* in the same way: commit and restart transactions where needed.
@@ -108,13 +94,17 @@ cluster(char *oldrelname, char *oldindexname)
10894
* of the initial transaction.
10995
*/
11096

111-
OldHeap=heap_openr(oldrelname,AccessExclusiveLock);
97+
OldHeap=heap_openr(saveoldrelname,AccessExclusiveLock);
11298
OIDOldHeap=RelationGetRelid(OldHeap);
11399

114-
OldIndex=index_openr(oldindexname);/* Open old index relation*/
100+
OldIndex=index_openr(saveoldindexname);/* Open old index relation*/
115101
LockRelation(OldIndex,AccessExclusiveLock);
116102
OIDOldIndex=RelationGetRelid(OldIndex);
117103

104+
/*
105+
* XXX Should check that index is in fact an index on this relation?
106+
*/
107+
118108
heap_close(OldHeap,NoLock);/* do NOT give up the locks */
119109
index_close(OldIndex);
120110

@@ -130,7 +120,6 @@ cluster(char *oldrelname, char *oldindexname)
130120
OIDNewHeap=RelationGetRelid(NewHeap);
131121
strcpy(NewHeapName,RelationGetRelationName(NewHeap));
132122

133-
134123
/* To make the new heap visible (which is until now empty). */
135124
CommandCounterIncrement();
136125

@@ -150,23 +139,14 @@ cluster(char *oldrelname, char *oldindexname)
150139
CommitTransactionCommand();
151140
StartTransactionCommand();
152141

153-
154142
/* Destroy old heap (along with its index) and rename new. */
155-
heap_drop_with_catalog(oldrelname);
143+
heap_drop_with_catalog(saveoldrelname);
156144

157145
CommitTransactionCommand();
158146
StartTransactionCommand();
159147

160148
renamerel(NewHeapName,saveoldrelname);
161-
TypeRename(NewHeapName,saveoldrelname);
162-
163149
renamerel(NewIndexName,saveoldindexname);
164-
165-
/*
166-
* Again flush all the buffers. XXX perhaps not needed?
167-
*/
168-
CommitTransactionCommand();
169-
StartTransactionCommand();
170150
}
171151

172152
staticRelation
@@ -298,6 +278,8 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap)
298278
Old_pg_index_Form->indisunique,
299279
Old_pg_index_Form->indisprimary);
300280

281+
setRelhasindexInplace(OIDNewHeap, true, false);
282+
301283
index_close(OldIndex);
302284
heap_close(NewHeap,AccessExclusiveLock);
303285
}
@@ -311,31 +293,31 @@ rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex)
311293
LocalOldIndex;
312294
IndexScanDescScanDesc;
313295
RetrieveIndexResultScanResult;
314-
HeapTupleDataLocalHeapTuple;
315-
BufferLocalBuffer;
316-
OidOIDNewHeapInsert;
317296

318297
/*
319298
* Open the relations I need. Scan through the OldHeap on the OldIndex
320299
* and insert each tuple into the NewHeap.
321300
*/
322301
LocalNewHeap=heap_open(OIDNewHeap,AccessExclusiveLock);
323302
LocalOldHeap=heap_open(OIDOldHeap,AccessExclusiveLock);
324-
LocalOldIndex=(Relation)index_open(OIDOldIndex);
303+
LocalOldIndex=index_open(OIDOldIndex);
325304

326305
ScanDesc=index_beginscan(LocalOldIndex, false,0, (ScanKey)NULL);
327306

328307
while ((ScanResult=index_getnext(ScanDesc,ForwardScanDirection))!=NULL)
329308
{
309+
HeapTupleDataLocalHeapTuple;
310+
BufferLocalBuffer;
330311

331312
LocalHeapTuple.t_self=ScanResult->heap_iptr;
332313
LocalHeapTuple.t_datamcxt=NULL;
333314
LocalHeapTuple.t_data=NULL;
334315
heap_fetch(LocalOldHeap,SnapshotNow,&LocalHeapTuple,&LocalBuffer);
335-
OIDNewHeapInsert=heap_insert(LocalNewHeap,&LocalHeapTuple);
336-
pfree(ScanResult);
316+
heap_insert(LocalNewHeap,&LocalHeapTuple);
337317
ReleaseBuffer(LocalBuffer);
318+
pfree(ScanResult);
338319
}
320+
339321
index_endscan(ScanDesc);
340322

341323
index_close(LocalOldIndex);

‎src/backend/commands/rename.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.42 2000/04/12 17:14:59 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.43 2000/05/11 03:54:18 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -18,6 +18,7 @@
1818

1919
#include"access/heapam.h"
2020
#include"catalog/catname.h"
21+
#include"catalog/pg_type.h"
2122
#include"utils/syscache.h"
2223
#include"catalog/heap.h"
2324
#include"catalog/indexing.h"
@@ -27,6 +28,8 @@
2728
#include"storage/smgr.h"
2829
#include"optimizer/prep.h"
2930
#include"utils/acl.h"
31+
#include"utils/relcache.h"
32+
3033

3134
/*
3235
*renameatt- changes the name of a attribute in a relation
@@ -180,6 +183,7 @@ renamerel(const char *oldrelname, const char *newrelname)
180183
Relationtargetrelation;
181184
Relationrelrelation;/* for RELATION relation */
182185
HeapTupleoldreltup;
186+
charrelkind;
183187
charoldpath[MAXPGPATH],
184188
newpath[MAXPGPATH],
185189
toldpath[MAXPGPATH+10],
@@ -194,11 +198,20 @@ renamerel(const char *oldrelname, const char *newrelname)
194198
elog(ERROR,"renamerel: Illegal class name: \"%s\" -- pg_ is reserved for system catalogs",
195199
newrelname);
196200

201+
/*
202+
* Instead of using heap_openr(), do it the hard way, so that we
203+
* can rename indexes as well as regular relations.
204+
*/
205+
targetrelation=RelationNameGetRelation(oldrelname);
206+
207+
if (!RelationIsValid(targetrelation))
208+
elog(ERROR,"Relation '%s' does not exist",oldrelname);
209+
197210
/*
198211
* Grab an exclusive lock on the target table, which we will NOT
199212
* release until end of transaction.
200213
*/
201-
targetrelation=heap_openr(oldrelname,AccessExclusiveLock);
214+
LockRelation(targetrelation,AccessExclusiveLock);
202215

203216
/* ----------------
204217
*RENAME TABLE within a transaction block is dangerous, because
@@ -215,6 +228,8 @@ renamerel(const char *oldrelname, const char *newrelname)
215228
if (IsTransactionBlock()&& !targetrelation->rd_myxactonly)
216229
elog(NOTICE,"Caution: RENAME TABLE cannot be rolled back, so don't abort now");
217230

231+
relkind=targetrelation->rd_rel->relkind;
232+
218233
/*
219234
* Flush all blocks of the relation out of the buffer pool. We need
220235
* this because the blocks are marked with the relation's name as well
@@ -304,4 +319,10 @@ renamerel(const char *oldrelname, const char *newrelname)
304319
CatalogCloseIndices(Num_pg_class_indices,irelations);
305320

306321
heap_close(relrelation,RowExclusiveLock);
322+
323+
/*
324+
* Also rename the associated type, if any.
325+
*/
326+
if (relkind!=RELKIND_INDEX)
327+
TypeRename(oldrelname,newrelname);
307328
}

‎src/backend/tcop/utility.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.87 2000/04/25 10:38:38 inoue Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.88 2000/05/11 03:54:18 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
1717
#include"postgres.h"
1818

1919
#include"access/heapam.h"
2020
#include"catalog/catalog.h"
21-
#include"catalog/pg_type.h"
2221
#include"commands/async.h"
2322
#include"commands/cluster.h"
2423
#include"commands/command.h"
@@ -315,8 +314,6 @@ ProcessUtility(Node *parsetree,
315314
*/
316315
renamerel(relname,/* old name */
317316
stmt->newname);/* new name */
318-
TypeRename(relname,/* old name */
319-
stmt->newname);/* new name */
320317
}
321318
else
322319
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp