*** ./src/backend/commands/command.c.orig	Sat Feb 23 11:54:18 2002
--- ./src/backend/commands/command.c	Sun Feb 24 16:07:08 2002
***************
*** 56,61 ****
--- 56,62 ----
  
  static void drop_default(Oid relid, int16 attnum);
  static bool needs_toast_table(Relation rel);
+ static char * get_relation_name(Oid);
  
  
  /* --------------------------------
***************
*** 1509,1514 ****
--- 1510,1533 ----
  
  }
  
+ 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;
+ }
  
  /*
   * ALTER TABLE OWNER
***************
*** 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);
  
--- 1552,1558 ----
  	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;
  
--- 1577,1583 ----
  	}
  
  	/*
! 	 * 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
--- 1589,1629 ----
  	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_relation_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
