*** ./src/backend/commands/command.c.orig	Sat Feb 23 11:54:18 2002
--- ./src/backend/commands/command.c	Sun Feb 24 16:56:21 2002
***************
*** 53,63 ****
  #include "utils/relcache.h"
  #include "utils/temprel.h"
  
- 
  static void drop_default(Oid relid, int16 attnum);
  static bool needs_toast_table(Relation rel);
  
- 
  /* --------------------------------
   *		PortalCleanup
   * --------------------------------
--- 53,61 ----
***************
*** 1509,1515 ****
  
  }
  
- 
  /*
   * ALTER TABLE OWNER
   */
--- 1507,1512 ----
***************
*** 1533,1539 ****
  	newOwnerSysid = get_usesysid(newOwnerName);
  
  	/*
! 	 * find the table's entry in pg_class and make a modifiable copy
  	 */
  	class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
  
--- 1530,1536 ----
  	newOwnerSysid = get_usesysid(newOwnerName);
  
  	/*
! 	 * find the relation's entry in pg_class and make a modifiable copy
  	 */
  	class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
  
***************
*** 1558,1564 ****
  	}
  
  	/*
! 	 * modify the table's entry and write to the heap
  	 */
  	((Form_pg_class) GETSTRUCT(tuple))->relowner = newOwnerSysid;
  
--- 1555,1561 ----
  	}
  
  	/*
! 	 * modify the relation's owner and write to the heap
  	 */
  	((Form_pg_class) GETSTRUCT(tuple))->relowner = newOwnerSysid;
  
***************
*** 1570,1581 ****
  	CatalogCloseIndices(Num_pg_class_indices, idescs);
  
  	/*
  	 * unlock everything and return
  	 */
  	heap_freetuple(tuple);
  	heap_close(class_rel, NoLock);
  }
- 
  
  /*
   * ALTER TABLE CREATE TOAST TABLE
--- 1567,1607 ----
  	CatalogCloseIndices(Num_pg_class_indices, idescs);
  
  	/*
+ 	 * If we are operating on a table, also change the ownership
+ 	 * of all of its indexes.
+ 	 */
+ 	if ((((Form_pg_class) GETSTRUCT(tuple))->relkind) == RELKIND_RELATION)
+ 	{
+ 		Relation target_rel;
+ 		List *index_oid_list, *i;
+ 
+ 		target_rel = heap_openr(relationName, RowExclusiveLock);
+ 		/* Find all the indexes belonging to this relation */
+ 		index_oid_list = RelationGetIndexList(target_rel);
+         heap_close(target_rel, RowExclusiveLock);
+ 
+ 		/* For each index, resursively change its ownership */
+ 		foreach (i, index_oid_list)
+ 		{
+ 			Oid indexoid;
+ 			char *index_relname;
+ 
+ 			indexoid = lfirsti(i);
+ 			index_relname = get_rel_name(indexoid);
+ 			AlterTableOwner(index_relname, newOwnerName);
+ 
+ 			pfree(index_relname);
+ 		}
+ 
+ 		freeList(index_oid_list);
+ 	}
+ 
+ 	/*
  	 * unlock everything and return
  	 */
  	heap_freetuple(tuple);
  	heap_close(class_rel, NoLock);
  }
  
  /*
   * ALTER TABLE CREATE TOAST TABLE
*** ./src/backend/utils/adt/ruleutils.c.orig	Sun Feb 24 16:56:45 2002
--- ./src/backend/utils/adt/ruleutils.c	Sun Feb 24 16:57:12 2002
***************
*** 141,147 ****
  				 StringInfo buf);
  static bool tleIsArrayAssign(TargetEntry *tle);
  static char *quote_identifier(char *ident);
- static char *get_relation_name(Oid relid);
  static char *get_relid_attribute_name(Oid relid, AttrNumber attnum);
  
  #define only_marker(rte)  ((rte)->inh ? "" : "ONLY ")
--- 141,146 ----
***************
*** 752,758 ****
  
  	/* The relation the rule is fired on */
  	appendStringInfo(buf, " TO %s",
! 					 quote_identifier(get_relation_name(ev_class)));
  	if (ev_attr > 0)
  		appendStringInfo(buf, ".%s",
  					  quote_identifier(get_relid_attribute_name(ev_class,
--- 751,757 ----
  
  	/* The relation the rule is fired on */
  	appendStringInfo(buf, " TO %s",
! 					 quote_identifier(get_rel_name(ev_class)));
  	if (ev_attr > 0)
  		appendStringInfo(buf, ".%s",
  					  quote_identifier(get_relid_attribute_name(ev_class,
***************
*** 2696,2725 ****
  	sprintf(result, "\"%s\"", ident);
  	return result;
  }
- 
- /* ----------
-  * get_relation_name			- Get a relation name by Oid
-  * ----------
-  */
- static char *
- get_relation_name(Oid relid)
- {
- 	HeapTuple	classtup;
- 	Form_pg_class classStruct;
- 	char	   *result;
- 
- 	classtup = SearchSysCache(RELOID,
- 							  ObjectIdGetDatum(relid),
- 							  0, 0, 0);
- 	if (!HeapTupleIsValid(classtup))
- 		elog(ERROR, "cache lookup of relation %u failed", relid);
- 
- 	classStruct = (Form_pg_class) GETSTRUCT(classtup);
- 	result = pstrdup(NameStr(classStruct->relname));
- 	ReleaseSysCache(classtup);
- 	return result;
- }
- 
  
  /* ----------
   * get_relid_attribute_name
--- 2695,2700 ----
