Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d516751

Browse files
committed
Make renamerel take an OID, not a RangeVar, to identify the relation
to rename. Avoids some corner-case bugs in cluster.c, improves consistency with renameatt.
1 parent 3114102 commit d516751

File tree

4 files changed

+62
-85
lines changed

4 files changed

+62
-85
lines changed

src/backend/commands/cluster.c

Lines changed: 22 additions & 26 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.76 2002/03/31 06:26:30 tgl Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.77 2002/03/31 07:49:30 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -37,8 +37,9 @@
3737
#include "utils/syscache.h"
3838

3939

40-
static Oid copy_heap(Oid OIDOldHeap, char *NewName);
41-
static void copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName);
40+
static Oid copy_heap(Oid OIDOldHeap, const char *NewName);
41+
static Oid copy_index(Oid OIDOldIndex, Oid OIDNewHeap,
42+
const char *NewIndexName);
4243
static void rebuildheap(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex);
4344

4445
/*
@@ -58,13 +59,12 @@ cluster(RangeVar *oldrelation, char *oldindexname)
5859
{
5960
Oid OIDOldHeap,
6061
OIDOldIndex,
61-
OIDNewHeap;
62+
OIDNewHeap,
63+
OIDNewIndex;
6264
Relation OldHeap,
6365
OldIndex;
6466
char NewHeapName[NAMEDATALEN];
6567
char NewIndexName[NAMEDATALEN];
66-
RangeVar *NewHeap;
67-
RangeVar *NewIndex;
6868

6969
/*
7070
* We grab exclusive access to the target rel and index for the
@@ -115,7 +115,7 @@ cluster(RangeVar *oldrelation, char *oldindexname)
115115
/* Create new index over the tuples of the new heap. */
116116
snprintf(NewIndexName, NAMEDATALEN, "temp_%u", OIDOldIndex);
117117

118-
copy_index(OIDOldIndex, OIDNewHeap, NewIndexName);
118+
OIDNewIndex = copy_index(OIDOldIndex, OIDNewHeap, NewIndexName);
119119

120120
CommandCounterIncrement();
121121

@@ -124,23 +124,16 @@ cluster(RangeVar *oldrelation, char *oldindexname)
124124

125125
CommandCounterIncrement();
126126

127-
/* XXX ugly, and possibly wrong in the presence of schemas... */
128-
/* would be better to pass OIDs to renamerel. */
129-
NewHeap = copyObject(oldrelation);
130-
NewHeap->relname = NewHeapName;
131-
NewIndex = copyObject(oldrelation);
132-
NewIndex->relname = NewIndexName;
133-
134-
renamerel(NewHeap, oldrelation->relname);
127+
renamerel(OIDNewHeap, oldrelation->relname);
135128

136129
/* This one might be unnecessary, but let's be safe. */
137130
CommandCounterIncrement();
138131

139-
renamerel(NewIndex, oldindexname);
132+
renamerel(OIDNewIndex, oldindexname);
140133
}
141134

142135
static Oid
143-
copy_heap(Oid OIDOldHeap, char *NewName)
136+
copy_heap(Oid OIDOldHeap, const char *NewName)
144137
{
145138
TupleDesc OldHeapDesc,
146139
tupdesc;
@@ -181,9 +174,10 @@ copy_heap(Oid OIDOldHeap, char *NewName)
181174
return OIDNewHeap;
182175
}
183176

184-
static void
185-
copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName)
177+
static Oid
178+
copy_index(Oid OIDOldIndex, Oid OIDNewHeap, const char *NewIndexName)
186179
{
180+
Oid OIDNewIndex;
187181
Relation OldIndex,
188182
NewHeap;
189183
IndexInfo *indexInfo;
@@ -198,19 +192,21 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap, char *NewIndexName)
198192
*/
199193
indexInfo = BuildIndexInfo(OldIndex->rd_index);
200194

201-
index_create(OIDNewHeap,
202-
NewIndexName,
203-
indexInfo,
204-
OldIndex->rd_rel->relam,
205-
OldIndex->rd_index->indclass,
206-
OldIndex->rd_index->indisprimary,
207-
allowSystemTableMods);
195+
OIDNewIndex = index_create(OIDNewHeap,
196+
NewIndexName,
197+
indexInfo,
198+
OldIndex->rd_rel->relam,
199+
OldIndex->rd_index->indclass,
200+
OldIndex->rd_index->indisprimary,
201+
allowSystemTableMods);
208202

209203
setRelhasindex(OIDNewHeap, true,
210204
OldIndex->rd_index->indisprimary, InvalidOid);
211205

212206
index_close(OldIndex);
213207
heap_close(NewHeap, NoLock);
208+
209+
return OIDNewIndex;
214210
}
215211

216212

src/backend/commands/rename.c

Lines changed: 32 additions & 39 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.67 2002/03/31 06:26:30 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.68 2002/03/31 07:49:30 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -69,8 +69,8 @@ static void update_ri_trigger_args(Oid relid,
6969
*/
7070
void
7171
renameatt(Oid relid,
72-
char *oldattname,
73-
char *newattname,
72+
const char *oldattname,
73+
const char *newattname,
7474
bool recurse)
7575
{
7676
Relation targetrelation;
@@ -250,51 +250,36 @@ renameatt(Oid relid,
250250
* renamerel - change the name of a relation
251251
*/
252252
void
253-
renamerel(const RangeVar *relation, const char *newrelname)
253+
renamerel(Oid relid, const char *newrelname)
254254
{
255255
Relation targetrelation;
256256
Relation relrelation; /* for RELATION relation */
257257
HeapTuple reltup;
258258
Oid namespaceId;
259-
Oid reloid;
260259
char relkind;
261260
bool relhastriggers;
262261
Relation irelations[Num_pg_class_indices];
263262

264-
if (!allowSystemTableMods && IsSystemRelationName(relation->relname))
265-
elog(ERROR, "renamerel: system relation \"%s\" may not be renamed",
266-
relation->relname);
267-
268-
if (!allowSystemTableMods && IsSystemRelationName(newrelname))
269-
elog(ERROR, "renamerel: Illegal class name: \"%s\" -- pg_ is reserved for system catalogs",
270-
newrelname);
271-
272263
/*
273264
* Grab an exclusive lock on the target table or index, which we will
274265
* NOT release until end of transaction.
275266
*/
276-
targetrelation = relation_openrv(relation, AccessExclusiveLock);
267+
targetrelation = relation_open(relid, AccessExclusiveLock);
277268

278269
namespaceId = RelationGetNamespace(targetrelation);
279-
reloid = RelationGetRelid(targetrelation);
280-
relkind = targetrelation->rd_rel->relkind;
281-
relhastriggers = (targetrelation->rd_rel->reltriggers > 0);
282270

283-
/*
284-
* Close rel, but keep exclusive lock!
285-
*/
286-
relation_close(targetrelation, NoLock);
271+
/* Validity checks */
272+
if (!allowSystemTableMods &&
273+
IsSystemRelationName(RelationGetRelationName(targetrelation)))
274+
elog(ERROR, "renamerel: system relation \"%s\" may not be renamed",
275+
RelationGetRelationName(targetrelation));
287276

288-
/*
289-
* Flush the relcache entry (easier than trying to change it at
290-
* exactly the right instant). It'll get rebuilt on next access to
291-
* relation.
292-
*
293-
* XXX What if relation is myxactonly?
294-
*
295-
* XXX this is probably not necessary anymore?
296-
*/
297-
RelationIdInvalidateRelationCacheByRelationId(reloid);
277+
if (!allowSystemTableMods && IsSystemRelationName(newrelname))
278+
elog(ERROR, "renamerel: Illegal class name: \"%s\" -- pg_ is reserved for system catalogs",
279+
newrelname);
280+
281+
relkind = targetrelation->rd_rel->relkind;
282+
relhastriggers = (targetrelation->rd_rel->reltriggers > 0);
298283

299284
/*
300285
* Find relation's pg_class tuple, and make sure newrelname isn't in
@@ -303,11 +288,11 @@ renamerel(const RangeVar *relation, const char *newrelname)
303288
relrelation = heap_openr(RelationRelationName, RowExclusiveLock);
304289

305290
reltup = SearchSysCacheCopy(RELOID,
306-
PointerGetDatum(reloid),
291+
PointerGetDatum(relid),
307292
0, 0, 0);
308293
if (!HeapTupleIsValid(reltup))
309294
elog(ERROR, "renamerel: relation \"%s\" does not exist",
310-
relation->relname);
295+
RelationGetRelationName(targetrelation));
311296

312297
if (get_relname_relid(newrelname, namespaceId) != InvalidOid)
313298
elog(ERROR, "renamerel: relation \"%s\" exists", newrelname);
@@ -332,7 +317,8 @@ renamerel(const RangeVar *relation, const char *newrelname)
332317
* Also rename the associated type, if any.
333318
*/
334319
if (relkind != RELKIND_INDEX)
335-
TypeRename(relation->relname, namespaceId, newrelname);
320+
TypeRename(RelationGetRelationName(targetrelation), namespaceId,
321+
newrelname);
336322

337323
/*
338324
* If it's a view, must also rename the associated ON SELECT rule.
@@ -342,7 +328,7 @@ renamerel(const RangeVar *relation, const char *newrelname)
342328
char *oldrulename,
343329
*newrulename;
344330

345-
oldrulename = MakeRetrieveViewRuleName(relation->relname);
331+
oldrulename = MakeRetrieveViewRuleName(RelationGetRelationName(targetrelation));
346332
newrulename = MakeRetrieveViewRuleName(newrelname);
347333
RenameRewriteRule(oldrulename, newrulename);
348334
}
@@ -353,14 +339,21 @@ renamerel(const RangeVar *relation, const char *newrelname)
353339
if (relhastriggers)
354340
{
355341
/* update tgargs where relname is primary key */
356-
update_ri_trigger_args(reloid,
357-
relation->relname, newrelname,
342+
update_ri_trigger_args(relid,
343+
RelationGetRelationName(targetrelation),
344+
newrelname,
358345
false, true);
359346
/* update tgargs where relname is foreign key */
360-
update_ri_trigger_args(reloid,
361-
relation->relname, newrelname,
347+
update_ri_trigger_args(relid,
348+
RelationGetRelationName(targetrelation),
349+
newrelname,
362350
true, true);
363351
}
352+
353+
/*
354+
* Close rel, but keep exclusive lock!
355+
*/
356+
relation_close(targetrelation, NoLock);
364357
}
365358

366359
/*

src/backend/tcop/utility.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.143 2002/03/31 06:26:31 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.144 2002/03/31 07:49:30 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -385,32 +385,20 @@ ProcessUtility(Node *parsetree,
385385

386386
CheckOwnership(stmt->relation, true);
387387

388-
/* ----------------
389-
* XXX using len == 3 to tell the difference
390-
* between "rename rel to newrel" and
391-
* "rename att in rel to newatt" will not
392-
* work soon because "rename type/operator/rule"
393-
* stuff is being added. - cim 10/24/90
394-
* ----------------
395-
* [another piece of amuzing but useless anecdote -- ay]
396-
*/
397388
if (stmt->column == NULL)
398389
{
399390
/*
400391
* rename relation
401-
*
402-
* Note: we also rename the "type" tuple corresponding to
403-
* the relation.
404392
*/
405-
renamerel(stmt->relation, /* old relation */
406-
stmt->newname); /* new name */
393+
renamerel(RangeVarGetRelid(stmt->relation, false),
394+
stmt->newname);
407395
}
408396
else
409397
{
410398
/*
411399
* rename attribute
412400
*/
413-
renameatt(RangeVarGetRelid(stmt->relation, false), /* relation */
401+
renameatt(RangeVarGetRelid(stmt->relation, false),
414402
stmt->column, /* old att name */
415403
stmt->newname, /* new att name */
416404
interpretInhOption(stmt->relation->inhOpt)); /* recursive? */

src/include/commands/rename.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: rename.h,v 1.15 2002/03/29 19:06:22 tgl Exp $
10+
* $Id: rename.h,v 1.16 2002/03/31 07:49:30 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#ifndef RENAME_H
1515
#define RENAME_H
1616

1717
extern void renameatt(Oid relid,
18-
char *oldattname,
19-
char *newattname,
18+
const char *oldattname,
19+
const char *newattname,
2020
bool recurse);
2121

22-
extern void renamerel(const RangeVar *relation,
22+
extern void renamerel(Oid relid,
2323
const char *newrelname);
2424

2525
#endif /* RENAME_H */

0 commit comments

Comments
 (0)