*** ./src/backend/commands/command.c.orig	Sat Feb 23 11:54:18 2002
--- ./src/backend/commands/command.c	Mon Feb 25 19:23:30 2002
***************
*** 53,62 ****
  #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,62 ----
  #include "utils/relcache.h"
  #include "utils/temprel.h"
  
  static void drop_default(Oid relid, int16 attnum);
  static bool needs_toast_table(Relation rel);
! static void AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId);
! static void CheckTupleType(Form_pg_class tuple_class);
  
  /* --------------------------------
   *		PortalCleanup
***************
*** 1509,1581 ****
  
  }
  
- 
  /*
   * ALTER TABLE OWNER
   */
  void
  AlterTableOwner(const char *relationName, const char *newOwnerName)
  {
! 	Relation	class_rel;
! 	HeapTuple	tuple;
! 	int32		newOwnerSysid;
! 	Relation	idescs[Num_pg_class_indices];
  
! 	/*
! 	 * first check that we are a superuser
! 	 */
  	if (!superuser())
  		elog(ERROR, "ALTER TABLE: permission denied");
  
! 	/*
! 	 * look up the new owner in pg_shadow and get the sysid
! 	 */
! 	newOwnerSysid = get_usesysid(newOwnerName);
  
! 	/*
! 	 * find the table's entry in pg_class and make a modifiable copy
! 	 */
! 	class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
  
! 	tuple = SearchSysCacheCopy(RELNAME,
! 							   PointerGetDatum(relationName),
  							   0, 0, 0);
  	if (!HeapTupleIsValid(tuple))
! 		elog(ERROR, "ALTER TABLE: relation \"%s\" not found",
! 			 relationName);
  
! 	switch (((Form_pg_class) GETSTRUCT(tuple))->relkind)
! 	{
! 		case RELKIND_RELATION:
! 		case RELKIND_INDEX:
! 		case RELKIND_VIEW:
! 		case RELKIND_SEQUENCE:
! 			/* ok to change owner */
! 			break;
! 		default:
! 			elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table, index, view, or sequence",
! 				 relationName);
! 	}
  
  	/*
! 	 * modify the table's entry and write to the heap
  	 */
! 	((Form_pg_class) GETSTRUCT(tuple))->relowner = newOwnerSysid;
! 
  	simple_heap_update(class_rel, &tuple->t_self, tuple);
  
  	/* Keep the catalog indices up to date */
  	CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
  	CatalogIndexInsert(idescs, Num_pg_class_indices, class_rel, tuple);
  	CatalogCloseIndices(Num_pg_class_indices, idescs);
! 
  	/*
! 	 * unlock everything and return
  	 */
  	heap_freetuple(tuple);
  	heap_close(class_rel, NoLock);
  }
  
  
  /*
   * ALTER TABLE CREATE TOAST TABLE
--- 1509,1599 ----
  
  }
  
  /*
   * ALTER TABLE OWNER
   */
  void
  AlterTableOwner(const char *relationName, const char *newOwnerName)
  {
! 	Oid relationOid;
! 	Relation relation;
! 	int32 newOwnerSysId;
  
! 	/* check that we are the superuser */
  	if (!superuser())
  		elog(ERROR, "ALTER TABLE: permission denied");
  
! 	/* lookup the OID of the target relation */
! 	relation = RelationNameGetRelation(relationName);
! 	relationOid = relation->rd_id;
! 	RelationClose(relation);
  
! 	/* lookup the sysid of the new owner */
! 	newOwnerSysId = get_usesysid(newOwnerName);
  
! 	/* do all the actual work */
! 	AlterTableOwnerId(relationOid, newOwnerSysId);
! }
! 
! static void
! AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId)
! {
! 	Relation		class_rel;
! 	HeapTuple		tuple;
! 	Relation		idescs[Num_pg_class_indices];
! 	Form_pg_class	tuple_class;
! 
! 	tuple = SearchSysCacheCopy(RELOID,
! 							   ObjectIdGetDatum(relationOid),
  							   0, 0, 0);
  	if (!HeapTupleIsValid(tuple))
! 		elog(ERROR, "ALTER TABLE: object ID %hd not found",
! 				relationOid);
  
! 	tuple_class = (Form_pg_class) GETSTRUCT(tuple);
! 
! 	/* Can we change the ownership of this tuple? */
! 	CheckTupleType(tuple_class);
  
  	/*
! 	 * Okay, this is a valid tuple: change its ownership and
! 	 * write to the heap.
  	 */
! 	class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
! 	tuple_class->relowner = newOwnerSysId;
  	simple_heap_update(class_rel, &tuple->t_self, tuple);
  
  	/* Keep the catalog indices up to date */
  	CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
  	CatalogIndexInsert(idescs, Num_pg_class_indices, class_rel, tuple);
  	CatalogCloseIndices(Num_pg_class_indices, idescs);
! 	
  	/*
! 	 * FIXME: This function does not change the ownership of any indexes
! 	 * which belong to this table. The proper fix for this is to remove
! 	 * the concept of "ownership" from indexes, as it is not useful.
  	 */
+ 
  	heap_freetuple(tuple);
  	heap_close(class_rel, NoLock);
  }
  
+ static void
+ CheckTupleType(Form_pg_class tuple_class)
+ {
+ 	switch (tuple_class->relkind)
+ 	{
+ 		case RELKIND_RELATION:
+ 		case RELKIND_INDEX:
+ 		case RELKIND_VIEW:
+ 		case RELKIND_SEQUENCE:
+ 			/* ok to change owner */
+ 			break;
+ 		default:
+ 			elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table, index, view, or sequence",
+ 				 NameStr(tuple_class->relname));
+ 	}
+ }
  
  /*
   * 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 ----
