Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6a7948

Browse files
committed
Make ALTER TABLE RENAME COLUMN update column names of indexes that
refer to the renamed column. Brent Verner, with a little help from tgl.
1 parent 7249562 commit b6a7948

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

src/backend/commands/rename.c

Lines changed: 64 additions & 5 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.57 2001/08/12 21:35:18 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.58 2001/10/08 18:40:04 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -18,6 +18,7 @@
1818

1919
#include "access/heapam.h"
2020
#include "catalog/catname.h"
21+
#include "catalog/pg_index.h"
2122
#include "catalog/pg_type.h"
2223
#include "catalog/heap.h"
2324
#include "catalog/indexing.h"
@@ -47,9 +48,6 @@
4748
* modify attname in attribute tuple
4849
* insert modified attribute in attribute catalog
4950
* delete original attribute from attribute catalog
50-
*
51-
* XXX Renaming an indexed attribute must (eventually) also change
52-
* the attribute name in the associated indexes.
5351
*/
5452
void
5553
renameatt(char *relname,
@@ -62,6 +60,8 @@ renameatt(char *relname,
6260
HeapTuple reltup,
6361
atttup;
6462
Oid relid;
63+
List *indexoidlist;
64+
List *indexoidscan;
6565

6666
/*
6767
* permissions checking. this would normally be done in utility.c,
@@ -83,7 +83,6 @@ renameatt(char *relname,
8383
*/
8484
targetrelation = heap_openr(relname, AccessExclusiveLock);
8585
relid = RelationGetRelid(targetrelation);
86-
heap_close(targetrelation, NoLock); /* close rel but keep lock! */
8786

8887
/*
8988
* if the 'recurse' flag is set then we are supposed to rename this
@@ -166,7 +165,67 @@ renameatt(char *relname,
166165
}
167166

168167
heap_freetuple(atttup);
168+
169+
/*
170+
* Update column names of indexes that refer to the column being renamed.
171+
*/
172+
indexoidlist = RelationGetIndexList(targetrelation);
173+
174+
foreach(indexoidscan, indexoidlist)
175+
{
176+
Oid indexoid = lfirsti(indexoidscan);
177+
HeapTuple indextup;
178+
179+
/*
180+
* First check to see if index is a functional index.
181+
* If so, its column name is a function name and shouldn't
182+
* be renamed here.
183+
*/
184+
indextup = SearchSysCache(INDEXRELID,
185+
ObjectIdGetDatum(indexoid),
186+
0, 0, 0);
187+
if (!HeapTupleIsValid(indextup))
188+
elog(ERROR, "renameatt: can't find index id %u", indexoid);
189+
if (OidIsValid(((Form_pg_index) GETSTRUCT(indextup))->indproc))
190+
{
191+
ReleaseSysCache(indextup);
192+
continue;
193+
}
194+
ReleaseSysCache(indextup);
195+
/*
196+
* Okay, look to see if any column name of the index matches
197+
* the old attribute name.
198+
*/
199+
atttup = SearchSysCacheCopy(ATTNAME,
200+
ObjectIdGetDatum(indexoid),
201+
PointerGetDatum(oldattname),
202+
0, 0);
203+
if (!HeapTupleIsValid(atttup))
204+
continue; /* Nope, so ignore it */
205+
206+
/*
207+
* Update the (copied) attribute tuple.
208+
*/
209+
StrNCpy(NameStr(((Form_pg_attribute) GETSTRUCT(atttup))->attname),
210+
newattname, NAMEDATALEN);
211+
212+
simple_heap_update(attrelation, &atttup->t_self, atttup);
213+
214+
/* keep system catalog indices current */
215+
{
216+
Relation irelations[Num_pg_attr_indices];
217+
218+
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, irelations);
219+
CatalogIndexInsert(irelations, Num_pg_attr_indices, attrelation, atttup);
220+
CatalogCloseIndices(Num_pg_attr_indices, irelations);
221+
}
222+
heap_freetuple(atttup);
223+
}
224+
225+
freeList(indexoidlist);
226+
169227
heap_close(attrelation, RowExclusiveLock);
228+
heap_close(targetrelation, NoLock); /* close rel but keep lock! */
170229
}
171230

172231
/*

0 commit comments

Comments
 (0)