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 */
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"
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 */
5452void
5553renameatt (char * relname ,
@@ -62,6 +60,8 @@ renameatt(char *relname,
6260HeapTuple reltup ,
6361atttup ;
6462Oid 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 */
8484targetrelation = heap_openr (relname ,AccessExclusiveLock );
8585relid = 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
168167heap_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+
169227heap_close (attrelation ,RowExclusiveLock );
228+ heap_close (targetrelation ,NoLock );/* close rel but keep lock! */
170229}
171230
172231/*