Index: doc/src/sgml/reference.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/reference.sgml,v
retrieving revision 1.49
diff -c -r1.49 reference.sgml
*** doc/src/sgml/reference.sgml	18 Jun 2004 21:24:04 -0000	1.49
--- doc/src/sgml/reference.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 45,55 ****
--- 45,58 ----
     &alterFunction;
     &alterGroup;
     &alterLanguage;
+    &alterOperator;
     &alterOperatorClass;
     &alterSchema;
     &alterSequence;
     &alterTable;
+    &alterTableSpace;
     &alterTrigger;
+    &alterType;
     &alterUser;
     &analyze;
     &begin;
Index: doc/src/sgml/ref/allfiles.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/allfiles.sgml,v
retrieving revision 1.58
diff -c -r1.58 allfiles.sgml
*** doc/src/sgml/ref/allfiles.sgml	18 Jun 2004 21:24:01 -0000	1.58
--- doc/src/sgml/ref/allfiles.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 13,23 ****
--- 13,26 ----
  <!entity alterFunction      system "alter_function.sgml">
  <!entity alterGroup         system "alter_group.sgml">
  <!entity alterLanguage      system "alter_language.sgml">
+ <!entity alterOperator      system "alter_operator.sgml">
  <!entity alterOperatorClass system "alter_opclass.sgml">
  <!entity alterSchema        system "alter_schema.sgml">
  <!entity alterSequence      system "alter_sequence.sgml">
  <!entity alterTable         system "alter_table.sgml">
+ <!entity alterTableSpace    system "alter_tablespace.sgml">
  <!entity alterTrigger       system "alter_trigger.sgml">
+ <!entity alterType          system "alter_type.sgml">
  <!entity alterUser          system "alter_user.sgml">
  <!entity analyze            system "analyze.sgml">
  <!entity begin              system "begin.sgml">
Index: doc/src/sgml/ref/alter_aggregate.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/alter_aggregate.sgml,v
retrieving revision 1.4
diff -c -r1.4 alter_aggregate.sgml
*** doc/src/sgml/ref/alter_aggregate.sgml	29 Nov 2003 19:51:38 -0000	1.4
--- doc/src/sgml/ref/alter_aggregate.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 21,26 ****
--- 21,27 ----
   <refsynopsisdiv>
  <synopsis>
  ALTER AGGREGATE <replaceable>name</replaceable> ( <replaceable>type</replaceable> ) RENAME TO <replaceable>newname</replaceable>
+ ALTER AGGREGATE <replaceable>name</replaceable> ( <replaceable>type</replaceable> ) OWNER TO <replaceable>newowner</replaceable>
  </synopsis>
   </refsynopsisdiv>
    
***************
*** 30,36 ****
    <para>
     <command>ALTER AGGREGATE</command> changes the definition of an
     aggregate function.  The only currently available functionality is to
!    rename the aggregate function.
    </para>
   </refsect1>
    
--- 31,38 ----
    <para>
     <command>ALTER AGGREGATE</command> changes the definition of an
     aggregate function.  The only currently available functionality is to
!    rename the aggregate function and change its owner.  You must be a
!    superuser to change an aggregate's owner.
    </para>
   </refsect1>
    
***************
*** 65,70 ****
--- 67,81 ----
       </para>
      </listitem>
     </varlistentry>
+ 
+    <varlistentry>
+     <term><replaceable class="parameter">newowner</replaceable></term>
+     <listitem>
+      <para>
+       The new owner of the aggregate function.
+      </para>
+     </listitem>
+    </varlistentry>
    </variablelist>
   </refsect1>
  
***************
*** 76,81 ****
--- 87,100 ----
     <type>integer</type> to <literal>my_average</literal>:
  <programlisting>
  ALTER AGGREGATE myavg(integer) RENAME TO my_average;
+ </programlisting>
+   </para>
+ 
+   <para>
+    To change the owner of the aggregate function <literal>myavg</literal> for type
+    <type>integer</type> to <literal>joe</literal>:
+ <programlisting>
+ ALTER AGGREGATE myavg(integer) OWNER TO joe;
  </programlisting>
    </para>
   </refsect1>
Index: doc/src/sgml/ref/alter_conversion.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/alter_conversion.sgml,v
retrieving revision 1.4
diff -c -r1.4 alter_conversion.sgml
*** doc/src/sgml/ref/alter_conversion.sgml	29 Nov 2003 19:51:38 -0000	1.4
--- doc/src/sgml/ref/alter_conversion.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 21,26 ****
--- 21,27 ----
   <refsynopsisdiv>
  <synopsis>
  ALTER CONVERSION <replaceable>name</replaceable> RENAME TO <replaceable>newname</replaceable>
+ ALTER CONVERSION <replaceable>name</replaceable> OWNER TO <replaceable>newowner</replaceable>
  </synopsis>
   </refsynopsisdiv>
    
***************
*** 29,36 ****
  
    <para>
     <command>ALTER CONVERSION</command> changes the definition of a
!    conversion.  The only currently available functionality is to rename the
!    conversion.
    </para>
   </refsect1>
    
--- 30,37 ----
  
    <para>
     <command>ALTER CONVERSION</command> changes the definition of a
!    conversion.  To change the owner of a conversion, you must be a
!    superuser.
    </para>
   </refsect1>
    
***************
*** 55,60 ****
--- 56,70 ----
       </para>
      </listitem>
     </varlistentry>
+ 
+    <varlistentry>
+     <term><replaceable class="parameter">newowner</replaceable></term>
+     <listitem>
+      <para>
+       The new owner of the conversion.
+      </para>
+     </listitem>
+    </varlistentry>
    </variablelist>
   </refsect1>
  
***************
*** 68,73 ****
--- 78,93 ----
  ALTER CONVERSION iso_8859_1_to_utf_8 RENAME TO latin1_to_unicode;
  </programlisting>
    </para>
+ 
+   <para>
+    To change the owner of the conversion <literal>iso_8859_1_to_utf_8</literal> to
+    <literal>joe</literal>:
+ <programlisting>
+ ALTER CONVERSION iso_8859_1_to_utf_8 OWNER TO joe;
+ </programlisting>
+   </para>
+  </refsect1>
+ 
   </refsect1>
  
   <refsect1>
Index: doc/src/sgml/ref/alter_function.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/alter_function.sgml,v
retrieving revision 1.4
diff -c -r1.4 alter_function.sgml
*** doc/src/sgml/ref/alter_function.sgml	29 Nov 2003 19:51:38 -0000	1.4
--- doc/src/sgml/ref/alter_function.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 21,26 ****
--- 21,27 ----
   <refsynopsisdiv>
  <synopsis>
  ALTER FUNCTION <replaceable>name</replaceable> ( [ <replaceable class="parameter">type</replaceable> [, ...] ] ) RENAME TO <replaceable>newname</replaceable>
+ ALTER FUNCTION <replaceable>name</replaceable> ( [ <replaceable class="parameter">type</replaceable> [, ...] ] ) OWNER TO <replaceable>newowner</replaceable>
  </synopsis>
   </refsynopsisdiv>
    
***************
*** 29,35 ****
  
    <para>
     <command>ALTER FUNCTION</command> changes the definition of a
!    function.  The only functionality is to rename the function.
    </para>
   </refsect1>
    
--- 30,37 ----
  
    <para>
     <command>ALTER FUNCTION</command> changes the definition of a
!    function.  To change the owner of a function, you must be a
!    superuser.
    </para>
   </refsect1>
    
***************
*** 63,68 ****
--- 65,81 ----
       </para>
      </listitem>
     </varlistentry>
+ 
+    <varlistentry>
+     <term><replaceable class="parameter">newowner</replaceable></term>
+     <listitem>
+      <para>
+       The new owner of the function.  Note that this will affect the
+       the user that the function will execute as, if it is marked
+       <literal>SECURITY DEFINER</literal>.
+      </para>
+     </listitem>
+    </varlistentry>
    </variablelist>
   </refsect1>
  
***************
*** 76,81 ****
--- 89,102 ----
  ALTER FUNCTION sqrt(integer) RENAME TO square_root;
  </programlisting>
    </para>
+ 
+   <para>
+    To change the owner of the function <literal>sqrt</literal> for type
+    <type>integer</type> to <literal>joe</literal>:
+ <programlisting>
+ ALTER FUNCTION sqrt(integer) OWNER TO joe;
+ </programlisting>
+   </para>
   </refsect1>
  
   <refsect1>
***************
*** 84,90 ****
    <para>
     There is an <command>ALTER FUNCTION</command> statement in the SQL
     standard, but it does not provide the option to rename the
!    function.
    </para>
   </refsect1>
  
--- 105,111 ----
    <para>
     There is an <command>ALTER FUNCTION</command> statement in the SQL
     standard, but it does not provide the option to rename the
!    function or change the owner.
    </para>
   </refsect1>
  
Index: doc/src/sgml/ref/alter_opclass.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/alter_opclass.sgml,v
retrieving revision 1.4
diff -c -r1.4 alter_opclass.sgml
*** doc/src/sgml/ref/alter_opclass.sgml	29 Nov 2003 19:51:38 -0000	1.4
--- doc/src/sgml/ref/alter_opclass.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 21,26 ****
--- 21,27 ----
   <refsynopsisdiv>
  <synopsis>
  ALTER OPERATOR CLASS <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> RENAME TO <replaceable>newname</replaceable>
+ ALTER OPERATOR CLASS <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> OWNER TO <replaceable>newowner</replaceable>
  </synopsis>
   </refsynopsisdiv>
    
***************
*** 29,36 ****
  
    <para>
     <command>ALTER OPERATOR CLASS</command> changes the definition of
!    an operator class.  The only functionality is to rename the
!    operator class.
    </para>
   </refsect1>
    
--- 30,37 ----
  
    <para>
     <command>ALTER OPERATOR CLASS</command> changes the definition of
!    an operator class.  You must be a superuser to change the owner
!    of an operator class.
    </para>
   </refsect1>
    
***************
*** 65,71 ****
       </para>
      </listitem>
     </varlistentry>
!   </variablelist>
   </refsect1>
  
   <refsect1>
--- 66,81 ----
       </para>
      </listitem>
     </varlistentry>
! 
!    <varlistentry>
!     <term><replaceable class="parameter">newowner</replaceable></term>
!     <listitem>
!      <para>
!       The new owner of the operator class.
!      </para>
!     </listitem>
!    </varlistentry>
!  </variablelist>
   </refsect1>
  
   <refsect1>
Index: doc/src/sgml/ref/alter_schema.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/alter_schema.sgml,v
retrieving revision 1.6
diff -c -r1.6 alter_schema.sgml
*** doc/src/sgml/ref/alter_schema.sgml	29 Nov 2003 19:51:38 -0000	1.6
--- doc/src/sgml/ref/alter_schema.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 21,26 ****
--- 21,27 ----
   <refsynopsisdiv>
  <synopsis>
  ALTER SCHEMA <replaceable>name</replaceable> RENAME TO <replaceable>newname</replaceable>
+ ALTER SCHEMA <replaceable>name</replaceable> OWNER TO <replaceable>newowner</replaceable>
  </synopsis>
   </refsynopsisdiv>
  
***************
*** 29,37 ****
  
    <para>
     <command>ALTER SCHEMA</command> changes the definition of a schema.
!    The only functionality is to rename the schema.  To rename a schema
!    you must own the schema and have the privilege
!    <literal>CREATE</literal> for the database.
    </para>
   </refsect1>
  
--- 30,38 ----
  
    <para>
     <command>ALTER SCHEMA</command> changes the definition of a schema.
!    To rename a schema you must own the schema and have the privilege
!    <literal>CREATE</literal> for the database.  To change the owner
!    of a schema, you must be a superuser.
    </para>
   </refsect1>
  
***************
*** 53,58 ****
--- 54,68 ----
      <listitem>
       <para>
        The new name of the schema
+      </para>
+     </listitem>
+    </varlistentry>
+ 
+    <varlistentry>
+     <term><replaceable class="parameter">newowner</replaceable></term>
+     <listitem>
+      <para>
+       The new owner of the schema.
       </para>
      </listitem>
     </varlistentry>
Index: doc/src/sgml/ref/create_operator.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/create_operator.sgml,v
retrieving revision 1.41
diff -c -r1.41 create_operator.sgml
*** doc/src/sgml/ref/create_operator.sgml	11 Dec 2003 20:13:53 -0000	1.41
--- doc/src/sgml/ref/create_operator.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 263,269 ****
  
    <para>
     Use <command>DROP OPERATOR</command> to delete user-defined
!    operators from a database.
    </para>
   </refsect1>
    
--- 263,270 ----
  
    <para>
     Use <command>DROP OPERATOR</command> to delete user-defined
!    operators from a database.  Use <command>ALTER OPERATOR</command>
!    to modify operators in a database.
    </para>
   </refsect1>
    
Index: doc/src/sgml/ref/create_tablespace.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/create_tablespace.sgml,v
retrieving revision 1.1
diff -c -r1.1 create_tablespace.sgml
*** doc/src/sgml/ref/create_tablespace.sgml	18 Jun 2004 06:13:05 -0000	1.1
--- doc/src/sgml/ref/create_tablespace.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 133,138 ****
--- 133,139 ----
     <member><xref linkend="sql-createindex" endterm="sql-createindex-title"></member>
     <member><xref linkend="sql-createsequence" endterm="sql-createsequence-title"></member>
     <member><xref linkend="sql-droptablespace" endterm="sql-droptablespace-title"></member>
+    <member><xref linkend="sql-altertablespace" endterm="sql-altertablespace-title"></member>
    </simplelist>
   </refsect1>
  
Index: doc/src/sgml/ref/create_type.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/create_type.sgml,v
retrieving revision 1.51
diff -c -r1.51 create_type.sgml
*** doc/src/sgml/ref/create_type.sgml	6 Jun 2004 00:41:25 -0000	1.51
--- doc/src/sgml/ref/create_type.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 546,551 ****
--- 546,552 ----
    <simplelist type="inline">
     <member><xref linkend="sql-createfunction" endterm="sql-createfunction-title"></member>
     <member><xref linkend="sql-droptype" endterm="sql-droptype-title"></member>
+    <member><xref linkend="sql-altertype" endterm="sql-altertype-title"></member>
    </simplelist>
   </refsect1>
  
Index: doc/src/sgml/ref/drop_operator.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/drop_operator.sgml,v
retrieving revision 1.24
diff -c -r1.24 drop_operator.sgml
*** doc/src/sgml/ref/drop_operator.sgml	2 Dec 2003 00:26:59 -0000	1.24
--- doc/src/sgml/ref/drop_operator.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 128,133 ****
--- 128,134 ----
  
    <simplelist type="inline">
     <member><xref linkend="sql-createoperator" endterm="sql-createoperator-title"></member>
+    <member><xref linkend="sql-alteroperator" endterm="sql-alteroperator-title"></member>
    </simplelist>
   </refsect1>
  
Index: doc/src/sgml/ref/drop_tablespace.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/drop_tablespace.sgml,v
retrieving revision 1.1
diff -c -r1.1 drop_tablespace.sgml
*** doc/src/sgml/ref/drop_tablespace.sgml	18 Jun 2004 06:13:05 -0000	1.1
--- doc/src/sgml/ref/drop_tablespace.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 80,85 ****
--- 80,86 ----
  
    <simplelist type="inline">
     <member><xref linkend="sql-createtablespace" endterm="sql-createtablespace-title"></member>
+    <member><xref linkend="sql-altertablespace" endterm="sql-altertablespace-title"></member>
    </simplelist>
   </refsect1>
  
Index: doc/src/sgml/ref/drop_type.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/ref/drop_type.sgml,v
retrieving revision 1.25
diff -c -r1.25 drop_type.sgml
*** doc/src/sgml/ref/drop_type.sgml	29 Nov 2003 19:51:38 -0000	1.25
--- doc/src/sgml/ref/drop_type.sgml	23 Jun 2004 04:40:49 -0000
***************
*** 95,100 ****
--- 95,101 ----
  
    <simplelist type="inline">
     <member><xref linkend="sql-createtype" endterm="sql-createtype-title"></member>
+    <member><xref linkend="sql-altertype" endterm="sql-altertype-title"></member>
    </simplelist>
   </refsect1>
  
Index: src/backend/commands/aggregatecmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/aggregatecmds.c,v
retrieving revision 1.18
diff -c -r1.18 aggregatecmds.c
*** src/backend/commands/aggregatecmds.c	26 May 2004 04:41:09 -0000	1.18
--- src/backend/commands/aggregatecmds.c	23 Jun 2004 04:40:49 -0000
***************
*** 288,290 ****
--- 288,349 ----
  	heap_close(rel, NoLock);
  	heap_freetuple(tup);
  }
+ 
+ /**
+  * Change aggregate owner
+  */
+ void
+ OwnerToAggregate(List *name, TypeName *basetype, AclId newOwnerSysId)
+ {
+ 	Oid			basetypeOid;
+ 	Oid			procOid;
+ 	HeapTuple	tup;
+ 	Form_pg_proc procForm;
+ 	Relation	rel;
+ 
+ 	/*
+ 	 * if a basetype is passed in, then attempt to find an aggregate for
+ 	 * that specific type.
+ 	 *
+ 	 * else attempt to find an aggregate with a basetype of ANYOID. This
+ 	 * means that the aggregate is to apply to all basetypes (eg, COUNT).
+ 	 */
+ 	if (basetype)
+ 		basetypeOid = typenameTypeId(basetype);
+ 	else
+ 		basetypeOid = ANYOID;
+ 
+ 	rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
+ 
+ 	procOid = find_aggregate_func(name, basetypeOid, false);
+ 
+ 	tup = SearchSysCacheCopy(PROCOID,
+ 							 ObjectIdGetDatum(procOid),
+ 							 0, 0, 0);
+ 	if (!HeapTupleIsValid(tup)) /* should not happen */
+ 		elog(ERROR, "cache lookup failed for function %u", procOid);
+ 	procForm = (Form_pg_proc) GETSTRUCT(tup);
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (procForm->proowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 		/* Modify the owner --- okay to scribble on tup because it's a copy */
+ 		procForm->proowner = newOwnerSysId;
+ 
+ 		simple_heap_update(rel, &tup->t_self, tup);
+ 
+ 		CatalogUpdateIndexes(rel, tup);
+ 	}
+ 
+ 	heap_close(rel, NoLock);
+ 	heap_freetuple(tup);
+ }
Index: src/backend/commands/alter.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/alter.c,v
retrieving revision 1.7
diff -c -r1.7 alter.c
*** src/backend/commands/alter.c	26 May 2004 04:41:09 -0000	1.7
--- src/backend/commands/alter.c	23 Jun 2004 04:40:49 -0000
***************
*** 25,31 ****
--- 25,33 ----
  #include "commands/proclang.h"
  #include "commands/schemacmds.h"
  #include "commands/tablecmds.h"
+ #include "commands/tablespace.h"
  #include "commands/trigger.h"
+ #include "commands/typecmds.h"
  #include "commands/user.h"
  #include "miscadmin.h"
  #include "parser/parse_clause.h"
***************
*** 34,40 ****
  #include "utils/lsyscache.h"
  #include "utils/syscache.h"
  
! 
  void
  ExecRenameStmt(RenameStmt *stmt)
  {
--- 36,45 ----
  #include "utils/lsyscache.h"
  #include "utils/syscache.h"
  
! /**
!  * Executes an ALTER OBJECT / RENAME TO statement.  Based on the object
!  * type, the function appropriate to that type is executed.
!  */
  void
  ExecRenameStmt(RenameStmt *stmt)
  {
***************
*** 74,79 ****
--- 79,88 ----
  			RenameSchema(stmt->subname, stmt->newname);
  			break;
  
+ 		case OBJECT_TABLESPACE:
+ 			RenameTableSpace(stmt->subname, stmt->newname);
+ 			break;
+ 
  		case OBJECT_USER:
  			RenameUser(stmt->subname, stmt->newname);
  			break;
***************
*** 131,135 ****
--- 140,193 ----
  		default:
  			elog(ERROR, "unrecognized rename stmt type: %d",
  				 (int) stmt->renameType);
+ 	}
+ }
+ 
+ /**
+  * Executes an ALTER OBJECT / OWNER TO statement.  Based on the object
+  * type, the function appropriate to that type is executed.
+  */
+ void
+ ExecOwnerStmt(OwnerStmt *stmt)
+ {
+ 	switch (stmt->ownerType)
+ 	{
+ 		case OBJECT_AGGREGATE:
+ 			OwnerToAggregate(stmt->object,
+ 							(TypeName *) linitial(stmt->objarg),
+ 							get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_CONVERSION:
+ 			OwnerToConversion(stmt->object, get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_FUNCTION:
+ 			OwnerToFunction(stmt->object, stmt->objarg, get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_OPERATOR:
+ 			OwnerToOperator(stmt->object, (TypeName *) linitial(stmt->objarg), (TypeName *) lsecond(stmt->objarg), get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_OPCLASS:
+ 			OwnerToOpClass(stmt->object, stmt->subname, get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_SCHEMA:
+ 			OwnerToSchema(stmt->subname, get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_TABLESPACE:
+ 			OwnerToTableSpace(stmt->subname, get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		case OBJECT_TYPE:
+ 			AlterTypeOwner(stmt->object, get_usesysid(stmt->newowner));
+ 			break;
+ 
+ 		default:
+ 			elog(ERROR, "unrecognized owner stmt type: %d",
+ 				 (int) stmt->ownerType);
  	}
  }
Index: src/backend/commands/conversioncmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/conversioncmds.c,v
retrieving revision 1.12
diff -c -r1.12 conversioncmds.c
*** src/backend/commands/conversioncmds.c	29 Nov 2003 19:51:47 -0000	1.12
--- src/backend/commands/conversioncmds.c	23 Jun 2004 04:40:49 -0000
***************
*** 171,173 ****
--- 171,225 ----
  	heap_close(rel, NoLock);
  	heap_freetuple(tup);
  }
+ 
+ /*
+  * Change conversion owner
+  */
+ void
+ OwnerToConversion(List *name, AclId newOwnerSysId)
+ {
+ 	Oid			conversionOid;
+ 	HeapTuple	tup;
+ 	Relation	rel;
+ 	Form_pg_conversion	convForm;
+ 
+ 	rel = heap_openr(ConversionRelationName, RowExclusiveLock);
+ 
+ 	conversionOid = FindConversionByName(name);
+ 	if (!OidIsValid(conversionOid))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_UNDEFINED_OBJECT),
+ 				 errmsg("conversion \"%s\" does not exist",
+ 						NameListToString(name))));
+ 
+ 	tup = SearchSysCacheCopy(CONOID,
+ 							 ObjectIdGetDatum(conversionOid),
+ 							 0, 0, 0);
+ 	if (!HeapTupleIsValid(tup)) /* should not happen */
+ 		elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
+ 
+ 	convForm = (Form_pg_conversion) GETSTRUCT(tup);
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (convForm->conowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 		/* Modify the owner --- okay to scribble on tup because it's a copy */
+ 		convForm->conowner = newOwnerSysId;
+ 
+ 		simple_heap_update(rel, &tup->t_self, tup);
+ 
+ 		CatalogUpdateIndexes(rel, tup);
+ 	}
+ 
+ 	heap_close(rel, NoLock);
+ 	heap_freetuple(tup);
+ }
Index: src/backend/commands/dbcommands.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/dbcommands.c,v
retrieving revision 1.136
diff -c -r1.136 dbcommands.c
*** src/backend/commands/dbcommands.c	18 Jun 2004 06:13:22 -0000	1.136
--- src/backend/commands/dbcommands.c	23 Jun 2004 04:40:49 -0000
***************
*** 774,779 ****
--- 774,780 ----
  	Relation	rel;
  	ScanKeyData scankey;
  	SysScanDesc scan;
+ 	Form_pg_database	datForm;
  
  	rel = heap_openr(DatabaseRelationName, RowExclusiveLock);
  	ScanKeyInit(&scankey,
***************
*** 790,808 ****
  
  	/* obtain sysid of proposed owner */
  	newdatdba = get_usesysid(newowner); /* will ereport if no such user */
  
! 	/* changing owner's database for someone else: must be superuser */
! 	/* note that the someone else need not have any permissions */
! 	if (!superuser())
! 		ereport(ERROR,
! 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
! 				 errmsg("must be superuser to change owner's database for another user")));
  
! 	/* change owner */
! 	newtuple = heap_copytuple(tuple);
! 	((Form_pg_database) GETSTRUCT(newtuple))->datdba = newdatdba;
! 	simple_heap_update(rel, &tuple->t_self, newtuple);
! 	CatalogUpdateIndexes(rel, newtuple);
  
  	systable_endscan(scan);
  	heap_close(rel, NoLock);
--- 791,817 ----
  
  	/* obtain sysid of proposed owner */
  	newdatdba = get_usesysid(newowner); /* will ereport if no such user */
+ 	newtuple = heap_copytuple(tuple);
+ 	datForm = (Form_pg_database) GETSTRUCT(newtuple);
  
! 	/* 
! 	 * If the new owner is the same as the existing owner, consider the
! 	 * command to have succeeded.  This is to be consistent with other objects.
! 	 */
! 	if (datForm->datdba != newdatdba)
! 	{
! 		/* changing owner's database for someone else: must be superuser */
! 		/* note that the someone else need not have any permissions */
! 		if (!superuser())
! 			ereport(ERROR,
! 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
! 					 errmsg("must be superuser to change owner's database for another user")));
  
! 		/* change owner */
! 		datForm->datdba = newdatdba;
! 		simple_heap_update(rel, &tuple->t_self, newtuple);
! 		CatalogUpdateIndexes(rel, newtuple);
! 	}
  
  	systable_endscan(scan);
  	heap_close(rel, NoLock);
Index: src/backend/commands/functioncmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/functioncmds.c,v
retrieving revision 1.48
diff -c -r1.48 functioncmds.c
*** src/backend/commands/functioncmds.c	16 Jun 2004 01:26:42 -0000	1.48
--- src/backend/commands/functioncmds.c	23 Jun 2004 04:40:50 -0000
***************
*** 723,728 ****
--- 723,782 ----
  	heap_freetuple(tup);
  }
  
+ /*
+  * Change function owner
+  */
+ void
+ OwnerToFunction(List *name, List *argtypes, AclId newOwnerSysId)
+ {
+ 	Oid			procOid;
+ 	HeapTuple	tup;
+ 	Form_pg_proc procForm;
+ 	Relation	rel;
+ 
+ 	rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
+ 
+ 	procOid = LookupFuncNameTypeNames(name, argtypes, false);
+ 
+ 	tup = SearchSysCacheCopy(PROCOID,
+ 							 ObjectIdGetDatum(procOid),
+ 							 0, 0, 0);
+ 	if (!HeapTupleIsValid(tup)) /* should not happen */
+ 		elog(ERROR, "cache lookup failed for function %u", procOid);
+ 	procForm = (Form_pg_proc) GETSTRUCT(tup);
+ 
+ 	if (procForm->proisagg)
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ 				 errmsg("\"%s\" is an aggregate function",
+ 						NameListToString(name)),
+ 		 errhint("Use ALTER AGGREGATE to change owner of aggregate functions.")));
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (procForm->proowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 		/* Modify the owner --- okay to scribble on tup because it's a copy */
+ 		procForm->proowner = newOwnerSysId;
+ 
+ 		simple_heap_update(rel, &tup->t_self, tup);
+ 
+ 		CatalogUpdateIndexes(rel, tup);
+ 	}
+ 
+ 	heap_close(rel, NoLock);
+ 	heap_freetuple(tup);
+ }
+ 
+ 
  
  /*
   * SetFunctionReturnType - change declared return type of a function
Index: src/backend/commands/opclasscmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/opclasscmds.c,v
retrieving revision 1.25
diff -c -r1.25 opclasscmds.c
*** src/backend/commands/opclasscmds.c	26 May 2004 04:41:11 -0000	1.25
--- src/backend/commands/opclasscmds.c	23 Jun 2004 04:40:50 -0000
***************
*** 871,873 ****
--- 871,962 ----
  	heap_close(rel, NoLock);
  	heap_freetuple(tup);
  }
+ 
+ /*
+  * Change opclass owner
+  */
+ void
+ OwnerToOpClass(List *name, const char *access_method, AclId newOwnerSysId)
+ {
+ 	Oid			opcOid;
+ 	Oid			amOid;
+ 	Oid			namespaceOid;
+ 	char	   *schemaname;
+ 	char	   *opcname;
+ 	HeapTuple	tup;
+ 	Relation	rel;
+ 	Form_pg_opclass	opcForm;
+ 
+ 	amOid = GetSysCacheOid(AMNAME,
+ 						   CStringGetDatum(access_method),
+ 						   0, 0, 0);
+ 	if (!OidIsValid(amOid))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_UNDEFINED_OBJECT),
+ 				 errmsg("access method \"%s\" does not exist",
+ 						access_method)));
+ 
+ 	rel = heap_openr(OperatorClassRelationName, RowExclusiveLock);
+ 
+ 	/*
+ 	 * Look up the opclass
+ 	 */
+ 	DeconstructQualifiedName(name, &schemaname, &opcname);
+ 
+ 	if (schemaname)
+ 	{
+ 		namespaceOid = LookupExplicitNamespace(schemaname);
+ 
+ 		tup = SearchSysCacheCopy(CLAAMNAMENSP,
+ 								 ObjectIdGetDatum(amOid),
+ 								 PointerGetDatum(opcname),
+ 								 ObjectIdGetDatum(namespaceOid),
+ 								 0);
+ 		if (!HeapTupleIsValid(tup))
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_UNDEFINED_OBJECT),
+ 					 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
+ 							opcname, access_method)));
+ 
+ 	}
+ 	else
+ 	{
+ 		opcOid = OpclassnameGetOpcid(amOid, opcname);
+ 		if (!OidIsValid(opcOid))
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_UNDEFINED_OBJECT),
+ 					 errmsg("operator class \"%s\" does not exist for access method \"%s\"",
+ 							opcname, access_method)));
+ 
+ 		tup = SearchSysCacheCopy(CLAOID,
+ 								 ObjectIdGetDatum(opcOid),
+ 								 0, 0, 0);
+ 		if (!HeapTupleIsValid(tup))		/* should not happen */
+ 			elog(ERROR, "cache lookup failed for opclass %u", opcOid);
+ 		namespaceOid = ((Form_pg_opclass) GETSTRUCT(tup))->opcnamespace;
+ 	}
+ 	opcForm = (Form_pg_opclass) GETSTRUCT(tup);
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (opcForm->opcowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 		/* Modify the owner --- okay to scribble on tup because it's a copy */
+ 		opcForm->opcowner = newOwnerSysId;
+ 
+ 		simple_heap_update(rel, &tup->t_self, tup);
+ 
+ 		CatalogUpdateIndexes(rel, tup);
+ 	}
+ 
+ 	heap_close(rel, NoLock);
+ 	heap_freetuple(tup);
+ }
Index: src/backend/commands/operatorcmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/operatorcmds.c,v
retrieving revision 1.16
diff -c -r1.16 operatorcmds.c
*** src/backend/commands/operatorcmds.c	26 May 2004 04:41:11 -0000	1.16
--- src/backend/commands/operatorcmds.c	23 Jun 2004 04:40:50 -0000
***************
*** 37,42 ****
--- 37,43 ----
  #include "access/heapam.h"
  #include "catalog/catname.h"
  #include "catalog/dependency.h"
+ #include "catalog/indexing.h"
  #include "catalog/namespace.h"
  #include "catalog/pg_operator.h"
  #include "commands/defrem.h"
***************
*** 263,265 ****
--- 264,317 ----
  
  	heap_close(relation, RowExclusiveLock);
  }
+ 
+ /*
+  * change operator owner
+  */
+ void
+ OwnerToOperator(List *name, TypeName *typeName1, TypeName *typeName2, AclId newOwnerSysId)
+ {
+ 	Oid			operOid;
+ 	HeapTuple	tup;
+ 	Relation	rel;
+ 	Form_pg_operator	oprForm;
+ 
+ 	rel = heap_openr(OperatorRelationName, RowExclusiveLock);
+ 
+ 	operOid = LookupOperNameTypeNames(name, typeName1, typeName2,
+ 									  false);
+ 
+ 	tup = SearchSysCacheCopy(OPEROID,
+ 						 ObjectIdGetDatum(operOid),
+ 						 0, 0, 0);
+ 	if (!HeapTupleIsValid(tup)) /* should not happen */
+ 		elog(ERROR, "cache lookup failed for operator %u", operOid);
+ 
+ 	oprForm = (Form_pg_operator) GETSTRUCT(tup);
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (oprForm->oprowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 		/* Modify the owner --- okay to scribble on tup because it's a copy */
+ 		oprForm->oprowner = newOwnerSysId;
+ 
+ 		simple_heap_update(rel, &tup->t_self, tup);
+ 
+ 		CatalogUpdateIndexes(rel, tup);
+ 	}
+ 
+ 	heap_close(rel, NoLock);
+ 	heap_freetuple(tup);
+ 
+ }
+ 
+ 
Index: src/backend/commands/schemacmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/schemacmds.c,v
retrieving revision 1.19
diff -c -r1.19 schemacmds.c
*** src/backend/commands/schemacmds.c	18 Jun 2004 06:13:23 -0000	1.19
--- src/backend/commands/schemacmds.c	23 Jun 2004 04:40:50 -0000
***************
*** 307,309 ****
--- 307,354 ----
  	heap_close(rel, NoLock);
  	heap_freetuple(tup);
  }
+ 
+ /*
+  * Change schema owner
+  */
+ void
+ OwnerToSchema(const char *oldname, AclId newOwnerSysId)
+ {
+ 	HeapTuple	tup;
+ 	Relation	rel;
+ 	Form_pg_namespace	nspForm;
+ 
+ 	rel = heap_openr(NamespaceRelationName, RowExclusiveLock);
+ 
+ 	tup = SearchSysCacheCopy(NAMESPACENAME,
+ 							 CStringGetDatum(oldname),
+ 							 0, 0, 0);
+ 	if (!HeapTupleIsValid(tup))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_UNDEFINED_SCHEMA),
+ 				 errmsg("schema \"%s\" does not exist", oldname)));
+ 	nspForm = (Form_pg_namespace) GETSTRUCT(tup);
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (nspForm->nspowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 		/* Modify the owner --- okay to scribble on tup because it's a copy */
+ 		nspForm->nspowner = newOwnerSysId;
+ 
+ 		simple_heap_update(rel, &tup->t_self, tup);
+ 
+ 		CatalogUpdateIndexes(rel, tup);
+ 	}
+ 
+ 	heap_close(rel, NoLock);
+ 	heap_freetuple(tup);
+ }
Index: src/backend/commands/tablecmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/tablecmds.c,v
retrieving revision 1.116
diff -c -r1.116 tablecmds.c
*** src/backend/commands/tablecmds.c	18 Jun 2004 06:13:23 -0000	1.116
--- src/backend/commands/tablecmds.c	23 Jun 2004 04:40:51 -0000
***************
*** 1921,1931 ****
  			pass = AT_PASS_MISC;
  			break;
  		case AT_ChangeOwner:	/* ALTER OWNER */
- 			/* check that we are the superuser */
- 			if (!superuser())
- 				ereport(ERROR,
- 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
- 				errmsg("must be superuser to alter owner")));
  			/* This command never recurses */
  			/* No command-specific prep needed */
  			pass = AT_PASS_MISC;
--- 1921,1926 ----
***************
*** 5097,5138 ****
  							NameStr(tuple_class->relname))));
  	}
  
! 	/*
! 	 * Okay, this is a valid tuple: change its ownership and write to the
! 	 * heap.
  	 */
! 	tuple_class->relowner = newOwnerSysId;
! 	simple_heap_update(class_rel, &tuple->t_self, tuple);
  
! 	/* Keep the catalog indexes up to date */
! 	CatalogUpdateIndexes(class_rel, tuple);
  
! 	/*
! 	 * If we are operating on a table, also change the ownership of any
! 	 * indexes that belong to the table, as well as the table's toast
! 	 * table (if it has one)
! 	 */
! 	if (tuple_class->relkind == RELKIND_RELATION ||
! 		tuple_class->relkind == RELKIND_TOASTVALUE)
! 	{
! 		List	   *index_oid_list;
! 		ListCell   *i;
  
! 		/* Find all the indexes belonging to this relation */
! 		index_oid_list = RelationGetIndexList(target_rel);
  
! 		/* For each index, recursively change its ownership */
! 		foreach(i, index_oid_list)
! 			ATExecChangeOwner(lfirst_oid(i), newOwnerSysId);
  
! 		list_free(index_oid_list);
! 	}
  
! 	if (tuple_class->relkind == RELKIND_RELATION)
! 	{
! 		/* If it has a toast table, recurse to change its ownership */
! 		if (tuple_class->reltoastrelid != InvalidOid)
! 			ATExecChangeOwner(tuple_class->reltoastrelid, newOwnerSysId);
  	}
  
  	heap_freetuple(tuple);
--- 5092,5146 ----
  							NameStr(tuple_class->relname))));
  	}
  
! 	/* 
! 	 * If the new owner is the same as the existing owner, consider the
! 	 * command to have succeeded.  This is for dump restoration purposes.
  	 */
! 	if (tuple_class->relowner != newOwnerSysId)
! 	{
! 		/* Otherwise, check that we are the superuser */
! 		if (!superuser())
! 			ereport(ERROR,
! 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
! 			errmsg("must be superuser to alter owner")));
  
! 		/*
! 		 * Okay, this is a valid tuple: change its ownership and write to the
! 		 * heap.
! 		 */
! 		tuple_class->relowner = newOwnerSysId;
! 		simple_heap_update(class_rel, &tuple->t_self, tuple);
  
! 		/* Keep the catalog indexes up to date */
! 		CatalogUpdateIndexes(class_rel, tuple);
  
! 		/*
! 		 * If we are operating on a table, also change the ownership of any
! 		 * indexes that belong to the table, as well as the table's toast
! 		 * table (if it has one)
! 		 */
! 		if (tuple_class->relkind == RELKIND_RELATION ||
! 			tuple_class->relkind == RELKIND_TOASTVALUE)
! 		{
! 			List	   *index_oid_list;
! 			ListCell   *i;
  
! 			/* Find all the indexes belonging to this relation */
! 			index_oid_list = RelationGetIndexList(target_rel);
  
! 			/* For each index, recursively change its ownership */
! 			foreach(i, index_oid_list)
! 				ATExecChangeOwner(lfirst_oid(i), newOwnerSysId);
  
! 			list_free(index_oid_list);
! 		}
! 
! 		if (tuple_class->relkind == RELKIND_RELATION)
! 		{
! 			/* If it has a toast table, recurse to change its ownership */
! 			if (tuple_class->reltoastrelid != InvalidOid)
! 				ATExecChangeOwner(tuple_class->reltoastrelid, newOwnerSysId);
! 		}
  	}
  
  	heap_freetuple(tuple);
Index: src/backend/commands/tablespace.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/tablespace.c,v
retrieving revision 1.3
diff -c -r1.3 tablespace.c
*** src/backend/commands/tablespace.c	21 Jun 2004 04:06:06 -0000	1.3
--- src/backend/commands/tablespace.c	23 Jun 2004 04:40:51 -0000
***************
*** 669,671 ****
--- 669,794 ----
  
  	return result;
  }
+ 
+ /*
+  * Rename tablespace
+  */
+ void
+ RenameTableSpace(const char *oldname, const char *newname)
+ {
+ 	Relation rel;
+ 	HeapScanDesc scanold, scannew;
+ 	HeapTuple	tup, tupnew;
+ 	HeapTuple	newtuple = NULL;
+ 	ScanKeyData	entry[1];
+ 
+ 	/* Search pg_tablespace */
+ 	rel = heap_openr(TableSpaceRelationName, RowExclusiveLock);
+ 
+ 	ScanKeyInit(&entry[0],
+ 				Anum_pg_tablespace_spcname,
+ 				BTEqualStrategyNumber, F_NAMEEQ,
+ 				CStringGetDatum(oldname));
+ 	scanold = heap_beginscan(rel, SnapshotNow, 1, entry);
+ 	tup = heap_getnext(scanold, ForwardScanDirection);
+ 
+ 	if (!HeapTupleIsValid(tup))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_UNDEFINED_OBJECT),
+ 				 errmsg("tablespace \"%s\" does not exist",
+ 						oldname)));
+ 
+ 	/* Keep the scan open as we might need to update the tuple */
+ 
+ 	/* Make sure the new name doesn't exist */
+ 	ScanKeyInit(&entry[0],
+ 				Anum_pg_tablespace_spcname,
+ 				BTEqualStrategyNumber, F_NAMEEQ,
+ 				CStringGetDatum(newname));
+ 	scannew = heap_beginscan(rel, SnapshotNow, 1, entry);
+ 	tupnew = heap_getnext(scannew, ForwardScanDirection);
+ 
+ 	if (HeapTupleIsValid(tupnew))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_DUPLICATE_OBJECT),
+ 				 errmsg("tablespace \"%s\" already exists",
+ 						newname)));
+ 	
+ 	heap_endscan(scannew);
+ 
+ 	/* Must be superuser */
+ 	if (!superuser())
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 				 errmsg("must be superuser to rename a tablespace")));
+ 
+ 	if (!allowSystemTableMods && IsReservedName(newname))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_RESERVED_NAME),
+ 				 errmsg("unacceptable tablespace name \"%s\"", newname),
+ 		errdetail("The prefix \"pg_\" is reserved for system tablespaces.")));
+ 
+ 	/* rename */
+ 	newtuple = heap_copytuple(tup);
+ 	namestrcpy(&(((Form_pg_tablespace) GETSTRUCT(newtuple))->spcname), newname);
+ 	simple_heap_update(rel, &tup->t_self, newtuple);
+ 	CatalogUpdateIndexes(rel, newtuple);
+ 
+ 	heap_endscan(scanold);
+ 	heap_close(rel, NoLock);
+ }
+ 
+ /*
+  * Change tablespace owner
+  */
+ void
+ OwnerToTableSpace(const char *tablespacename, AclId newOwnerSysId)
+ {
+ 	Relation rel;
+ 	HeapScanDesc scandesc;
+ 	ScanKeyData	entry[1];
+ 	Form_pg_tablespace	spcForm;
+ 	HeapTuple	tup;
+ 	HeapTuple	newtuple = NULL;
+ 
+ 	/* Search pg_tablespace */
+ 	rel = heap_openr(TableSpaceRelationName, RowExclusiveLock);
+ 
+ 	ScanKeyInit(&entry[0],
+ 				Anum_pg_tablespace_spcname,
+ 				BTEqualStrategyNumber, F_NAMEEQ,
+ 				CStringGetDatum(tablespacename));
+ 	scandesc = heap_beginscan(rel, SnapshotNow, 1, entry);
+ 	tup = heap_getnext(scandesc, ForwardScanDirection);
+ 
+ 	if (!HeapTupleIsValid(tup))
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_UNDEFINED_OBJECT),
+ 				 errmsg("tablespace \"%s\" does not exist",
+ 						tablespacename)));
+ 
+ 	newtuple = heap_copytuple(tup);
+ 	spcForm = (Form_pg_tablespace) GETSTRUCT(newtuple);
+ 
+ 	/* 
+ 	 * If the new owner is the same as the existing owner, consider the
+ 	 * command to have succeeded.  This is for dump restoration purposes.
+ 	 */
+ 	if (spcForm->spcowner != newOwnerSysId)
+ 	{
+ 		/* Otherwise, must be superuser to change object ownership */
+ 		if (!superuser())
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ 					 errmsg("must be superuser to change owner")));
+ 
+ 
+ 		/* Modify the owner */
+ 		spcForm->spcowner = newOwnerSysId;
+ 		simple_heap_update(rel, &tup->t_self, newtuple);
+ 		CatalogUpdateIndexes(rel, newtuple);
+ 	}
+ 
+ 	heap_endscan(scandesc);
+ 	heap_close(rel, NoLock);
+ }
Index: src/backend/commands/typecmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/typecmds.c,v
retrieving revision 1.60
diff -c -r1.60 typecmds.c
*** src/backend/commands/typecmds.c	18 Jun 2004 06:13:23 -0000	1.60
--- src/backend/commands/typecmds.c	23 Jun 2004 04:40:51 -0000
***************
*** 2042,2055 ****
  }
  
  /*
!  * ALTER DOMAIN .. OWNER TO
!  *
!  * Eventually this should allow changing ownership of other kinds of types,
!  * but some thought must be given to handling complex types.  (A table's
!  * rowtype probably shouldn't be allowed as target, but what of a standalone
!  * composite type?)
!  *
!  * Assumes that permission checks have been completed earlier.
   */
  void
  AlterTypeOwner(List *names, AclId newOwnerSysId)
--- 2042,2048 ----
  }
  
  /*
!  * Change the owner of a type or domain.
   */
  void
  AlterTypeOwner(List *names, AclId newOwnerSysId)
***************
*** 2084,2102 ****
  		elog(ERROR, "cache lookup failed for type %u", typeOid);
  	typTup = (Form_pg_type) GETSTRUCT(tup);
  
! 	/* Check that this is actually a domain */
! 	if (typTup->typtype != 'd')
  		ereport(ERROR,
  				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
! 				 errmsg("\"%s\" is not a domain",
  						TypeNameToString(typename))));
  
! 	/* Modify the owner --- okay to scribble on typTup because it's a copy */
! 	typTup->typowner = newOwnerSysId;
  
! 	simple_heap_update(rel, &tup->t_self, tup);
  
! 	CatalogUpdateIndexes(rel, tup);
  
  	/* Clean up */
  	heap_close(rel, RowExclusiveLock);
--- 2077,2138 ----
  		elog(ERROR, "cache lookup failed for type %u", typeOid);
  	typTup = (Form_pg_type) GETSTRUCT(tup);
  
! 	/* Check that this is actually a domain or composite type */
! 	if (typTup->typtype != 'b' && typTup->typtype != 'd' && typTup->typtype != 'c')
  		ereport(ERROR,
  				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
! 				 errmsg("\"%s\" is not a base, domain or composite type",
  						TypeNameToString(typename))));
  
! 	/* If it's a composite type, we need to check that it really is a 
!  	 * free-standing composite type, and not a table's underlying type */
! 	if (typTup->typtype == 'c')
! 	{
! 		Relation	class_rel;
! 		HeapTuple	tuple;
! 		Form_pg_class	tuple_class;
! 
! 		/* Get the pg_class tuple referred to by typrelid */
! 		class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
! 
! 		tuple = SearchSysCache(RELOID,
! 								   ObjectIdGetDatum(typTup->typrelid),
! 								   0, 0, 0);
! 		if (!HeapTupleIsValid(tuple))
! 			elog(ERROR, "cache lookup failed for relation %u", typTup->typrelid);
! 		tuple_class = (Form_pg_class) GETSTRUCT(tuple);
! 
! 		/* Check that the pg_class entry represents a standalon composite type */
! 		if (tuple_class->relkind != 'c')
! 			ereport(ERROR,
! 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
! 					 errmsg("\"%s\" is not a base, domain or composite type",
! 							TypeNameToString(typename))));
! 
! 		/* Clean up */
! 		ReleaseSysCache(tuple);
! 		heap_close(class_rel, RowExclusiveLock);
! 	}
! 
! 	/* 
! 	 * If the new owner is the same as the existing owner, consider the
! 	 * command to have succeeded.  This is for dump restoration purposes.
! 	 */
! 	if (typTup->typowner != newOwnerSysId)
! 	{
! 		/* Otherwise, must be superuser to change object ownership */
! 		if (!superuser())
! 			ereport(ERROR,
! 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
! 					 errmsg("must be superuser to change owner")));
  
! 		/* Modify the owner --- okay to scribble on typTup because it's a copy */
! 		typTup->typowner = newOwnerSysId;
  
! 		simple_heap_update(rel, &tup->t_self, tup);
! 
! 		CatalogUpdateIndexes(rel, tup);
! 	}
  
  	/* Clean up */
  	heap_close(rel, RowExclusiveLock);
Index: src/backend/nodes/copyfuncs.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/nodes/copyfuncs.c,v
retrieving revision 1.286
diff -c -r1.286 copyfuncs.c
*** src/backend/nodes/copyfuncs.c	18 Jun 2004 06:13:28 -0000	1.286
--- src/backend/nodes/copyfuncs.c	23 Jun 2004 04:40:51 -0000
***************
*** 1937,1942 ****
--- 1937,1957 ----
  	return newnode;
  }
  
+ static OwnerStmt *
+ _copyOwnerStmt(OwnerStmt *from)
+ {
+ 	OwnerStmt *newnode = makeNode(OwnerStmt);
+ 
+ 	COPY_NODE_FIELD(relation);
+ 	COPY_NODE_FIELD(object);
+ 	COPY_NODE_FIELD(objarg);
+ 	COPY_STRING_FIELD(subname);
+ 	COPY_STRING_FIELD(newowner);
+ 	COPY_SCALAR_FIELD(ownerType);
+ 
+ 	return newnode;
+ }
+ 
  static RuleStmt *
  _copyRuleStmt(RuleStmt *from)
  {
***************
*** 2873,2878 ****
--- 2888,2896 ----
  			break;
  		case T_RenameStmt:
  			retval = _copyRenameStmt(from);
+ 			break;
+ 		case T_OwnerStmt:
+ 			retval = _copyOwnerStmt(from);
  			break;
  		case T_RuleStmt:
  			retval = _copyRuleStmt(from);
Index: src/backend/nodes/equalfuncs.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/nodes/equalfuncs.c,v
retrieving revision 1.225
diff -c -r1.225 equalfuncs.c
*** src/backend/nodes/equalfuncs.c	18 Jun 2004 06:13:28 -0000	1.225
--- src/backend/nodes/equalfuncs.c	23 Jun 2004 04:40:51 -0000
***************
*** 992,997 ****
--- 992,1010 ----
  }
  
  static bool
+ _equalOwnerStmt(OwnerStmt *a, OwnerStmt *b)
+ {
+ 	COMPARE_NODE_FIELD(relation);
+ 	COMPARE_NODE_FIELD(object);
+ 	COMPARE_NODE_FIELD(objarg);
+ 	COMPARE_STRING_FIELD(subname);
+ 	COMPARE_STRING_FIELD(newowner);
+ 	COMPARE_SCALAR_FIELD(ownerType);
+ 
+ 	return true;
+ }
+ 
+ static bool
  _equalRuleStmt(RuleStmt *a, RuleStmt *b)
  {
  	COMPARE_NODE_FIELD(relation);
***************
*** 2007,2012 ****
--- 2020,2028 ----
  			break;
  		case T_RenameStmt:
  			retval = _equalRenameStmt(a, b);
+ 			break;
+ 		case T_OwnerStmt:
+ 			retval = _equalOwnerStmt(a, b);
  			break;
  		case T_RuleStmt:
  			retval = _equalRuleStmt(a, b);
Index: src/backend/parser/gram.y
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/parser/gram.y,v
retrieving revision 2.462
diff -c -r2.462 gram.y
*** src/backend/parser/gram.y	18 Jun 2004 06:13:31 -0000	2.462
--- src/backend/parser/gram.y	23 Jun 2004 04:40:52 -0000
***************
*** 152,158 ****
  		VariableResetStmt VariableSetStmt VariableShowStmt
  		ViewStmt CheckPointStmt CreateConversionStmt
  		DeallocateStmt PrepareStmt ExecuteStmt
! 		AlterDbOwnerStmt
  
  %type <node>	select_no_parens select_with_parens select_clause
  				simple_select
--- 152,158 ----
  		VariableResetStmt VariableSetStmt VariableShowStmt
  		ViewStmt CheckPointStmt CreateConversionStmt
  		DeallocateStmt PrepareStmt ExecuteStmt
! 		AlterDbOwnerStmt OwnerStmt
  
  %type <node>	select_no_parens select_with_parens select_clause
  				simple_select
***************
*** 543,548 ****
--- 543,549 ----
  			| LoadStmt
  			| LockStmt
  			| NotifyStmt
+ 			| OwnerStmt
  			| PrepareStmt
  			| ReindexStmt
  			| RemoveAggrStmt
***************
*** 3670,3676 ****
--- 3671,3762 ----
  					n->newname = $6;
  					$$ = (Node *)n;
  				}
+ 			| ALTER TABLESPACE name RENAME TO name
+ 				{
+ 					RenameStmt *n = makeNode(RenameStmt);
+ 					n->renameType = OBJECT_TABLESPACE;
+ 					n->subname = $3;
+ 					n->newname = $6;
+ 					$$ = (Node *)n;
+ 				}
  		;
+ 
+ /*****************************************************************************
+  *
+  * ALTER THING name OWNER TO newname.  
+  *
+  *****************************************************************************/
+ 
+ OwnerStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_AGGREGATE;
+ 					n->object = $3;
+ 					n->objarg = list_make1($5);
+ 					n->newowner = $9;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER CONVERSION_P any_name OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_CONVERSION;
+ 					n->object = $3;
+ 					n->newowner = $6;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER FUNCTION func_name func_args OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_FUNCTION;
+ 					n->object = $3;
+ 					n->objarg = extractArgTypes($4);
+ 					n->newowner = $7;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_OPERATOR;
+ 					n->object = $3;
+ 					n->objarg = $5;
+ 					n->newowner = $9;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER OPERATOR CLASS any_name USING access_method OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_OPCLASS;
+ 					n->object = $4;
+ 					n->subname = $6;
+ 					n->newowner = $9;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER SCHEMA name OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_SCHEMA;
+ 					n->subname = $3;
+ 					n->newowner = $6;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER TYPE_P any_name OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_TYPE;
+ 					n->object = $3;
+ 					n->newowner = $6;
+ 					$$ = (Node *)n;
+ 				}
+ 			| ALTER TABLESPACE name OWNER TO UserId
+ 				{
+ 					OwnerStmt *n = makeNode(OwnerStmt);
+ 					n->ownerType = OBJECT_TABLESPACE;
+ 					n->subname = $3;
+ 					n->newowner = $6;
+ 					$$ = (Node *)n;
+ 				}
+ 		;
+ 
  
  opt_column: COLUMN									{ $$ = COLUMN; }
  			| /*EMPTY*/								{ $$ = 0; }
Index: src/backend/tcop/utility.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/tcop/utility.c,v
retrieving revision 1.219
diff -c -r1.219 utility.c
*** src/backend/tcop/utility.c	18 Jun 2004 06:13:38 -0000	1.219
--- src/backend/tcop/utility.c	23 Jun 2004 04:40:53 -0000
***************
*** 242,247 ****
--- 242,248 ----
  		case T_AlterSeqStmt:
  		case T_AlterTableStmt:
  		case T_RenameStmt:
+ 		case T_OwnerStmt:
  		case T_AlterUserStmt:
  		case T_AlterUserSetStmt:
  		case T_CommentStmt:
***************
*** 527,532 ****
--- 528,537 ----
  			ExecRenameStmt((RenameStmt *) parsetree);
  			break;
  
+ 		case T_OwnerStmt:
+ 			ExecOwnerStmt((OwnerStmt *) parsetree);
+ 			break;
+ 
  		case T_AlterTableStmt:
  			AlterTable((AlterTableStmt *) parsetree);
  			break;
***************
*** 568,579 ****
  												  stmt->behavior);
  						break;
  					case 'U':	/* OWNER TO */
- 						/* check that we are the superuser */
- 						if (!superuser())
- 							ereport(ERROR,
- 								(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
- 							errmsg("must be superuser to alter owner")));
- 						/* get_usesysid raises an error if no such user */
  						AlterTypeOwner(stmt->typename,
  									   get_usesysid(stmt->name));
  						break;
--- 573,578 ----
***************
*** 1252,1257 ****
--- 1251,1285 ----
  					break;
  				case OBJECT_USER:
  					tag = "ALTER USER";
+ 					break;
+ 				default:
+ 					tag = "ALTER TABLE";
+ 			}
+ 			break;
+ 
+ 		case T_OwnerStmt:
+ 			switch (((OwnerStmt *) parsetree)->ownerType)
+ 			{
+ 				case OBJECT_AGGREGATE:
+ 					tag = "ALTER AGGREGATE";
+ 					break;
+ 				case OBJECT_CONVERSION:
+ 					tag = "ALTER CONVERSION";
+ 					break;
+ 				case OBJECT_DATABASE:
+ 					tag = "ALTER DATABASE";
+ 					break;
+ 				case OBJECT_FUNCTION:
+ 					tag = "ALTER FUNCTION";
+ 					break;
+ 				case OBJECT_OPCLASS:
+ 					tag = "ALTER OPERATOR CLASS";
+ 					break;
+ 				case OBJECT_SCHEMA:
+ 					tag = "ALTER SCHEMA";
+ 					break;
+ 				case OBJECT_TABLESPACE:
+ 					tag = "ALTER TABLESPACE";
  					break;
  				default:
  					tag = "ALTER TABLE";
Index: src/include/commands/alter.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/commands/alter.h,v
retrieving revision 1.3
diff -c -r1.3 alter.h
*** src/include/commands/alter.h	29 Nov 2003 22:40:59 -0000	1.3
--- src/include/commands/alter.h	23 Jun 2004 04:40:55 -0000
***************
*** 18,21 ****
--- 18,23 ----
  
  extern void ExecRenameStmt(RenameStmt *stmt);
  
+ extern void ExecOwnerStmt(OwnerStmt *stmt);
+ 
  #endif   /* ALTER_H */
Index: src/include/commands/conversioncmds.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/commands/conversioncmds.h,v
retrieving revision 1.6
diff -c -r1.6 conversioncmds.h
*** src/include/commands/conversioncmds.h	29 Nov 2003 22:40:59 -0000	1.6
--- src/include/commands/conversioncmds.h	23 Jun 2004 04:40:55 -0000
***************
*** 20,24 ****
--- 20,25 ----
  extern void CreateConversionCommand(CreateConversionStmt *parsetree);
  extern void DropConversionCommand(List *conversion_name, DropBehavior behavior);
  extern void RenameConversion(List *name, const char *newname);
+ extern void OwnerToConversion(List *name, AclId newOwnerSysId);
  
  #endif   /* CONVERSIONCMDS_H */
Index: src/include/commands/defrem.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/commands/defrem.h,v
retrieving revision 1.58
diff -c -r1.58 defrem.h
*** src/include/commands/defrem.h	18 Jun 2004 06:14:08 -0000	1.58
--- src/include/commands/defrem.h	23 Jun 2004 04:40:55 -0000
***************
*** 48,53 ****
--- 48,54 ----
  extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
  extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
  extern void RenameFunction(List *name, List *argtypes, const char *newname);
+ extern void OwnerToFunction(List *name, List *argtypes, AclId newOwnerSysId);
  extern void CreateCast(CreateCastStmt *stmt);
  extern void DropCast(DropCastStmt *stmt);
  extern void DropCastById(Oid castOid);
***************
*** 56,72 ****
--- 57,76 ----
  extern void DefineOperator(List *names, List *parameters);
  extern void RemoveOperator(RemoveOperStmt *stmt);
  extern void RemoveOperatorById(Oid operOid);
+ extern void OwnerToOperator(List *name, TypeName *typeName1, TypeName *typename2, AclId newOwnerSysId);
  
  /* commands/aggregatecmds.c */
  extern void DefineAggregate(List *names, List *parameters);
  extern void RemoveAggregate(RemoveAggrStmt *stmt);
  extern void RenameAggregate(List *name, TypeName *basetype, const char *newname);
+ extern void OwnerToAggregate(List *name, TypeName *basetype, AclId newOwnerSysId);
  
  /* commands/opclasscmds.c */
  extern void DefineOpClass(CreateOpClassStmt *stmt);
  extern void RemoveOpClass(RemoveOpClassStmt *stmt);
  extern void RemoveOpClassById(Oid opclassOid);
  extern void RenameOpClass(List *name, const char *access_method, const char *newname);
+ extern void OwnerToOpClass(List *name, const char *access_method, AclId newOwnerSysId); 
  
  /* support routines in commands/define.c */
  
Index: src/include/commands/schemacmds.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/commands/schemacmds.h,v
retrieving revision 1.6
diff -c -r1.6 schemacmds.h
*** src/include/commands/schemacmds.h	29 Nov 2003 22:40:59 -0000	1.6
--- src/include/commands/schemacmds.h	23 Jun 2004 04:40:55 -0000
***************
*** 23,27 ****
--- 23,28 ----
  extern void RemoveSchemaById(Oid schemaOid);
  
  extern void RenameSchema(const char *oldname, const char *newname);
+ extern void OwnerToSchema(const char *oldname, AclId newOwnerSysId);
  
  #endif   /* SCHEMACMDS_H */
Index: src/include/commands/tablespace.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/commands/tablespace.h,v
retrieving revision 1.1
diff -c -r1.1 tablespace.h
*** src/include/commands/tablespace.h	18 Jun 2004 06:14:08 -0000	1.1
--- src/include/commands/tablespace.h	23 Jun 2004 04:40:55 -0000
***************
*** 26,29 ****
--- 26,32 ----
  
  extern char *get_tablespace_name(Oid spc_oid);
  
+ extern void RenameTableSpace(const char *oldname, const char *newname);
+ extern void OwnerToTableSpace(const char *tablespacename, AclId newOwnerSysId);
+ 
  #endif   /* TABLESPACE_H */
Index: src/include/nodes/nodes.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/nodes/nodes.h,v
retrieving revision 1.158
diff -c -r1.158 nodes.h
*** src/include/nodes/nodes.h	18 Jun 2004 06:14:11 -0000	1.158
--- src/include/nodes/nodes.h	23 Jun 2004 04:40:55 -0000
***************
*** 225,230 ****
--- 225,231 ----
  	T_RemoveFuncStmt,
  	T_RemoveOperStmt,
  	T_RenameStmt,
+ 	T_OwnerStmt,
  	T_RuleStmt,
  	T_NotifyStmt,
  	T_ListenStmt,
Index: src/include/nodes/parsenodes.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/nodes/parsenodes.h,v
retrieving revision 1.259
diff -c -r1.259 parsenodes.h
*** src/include/nodes/parsenodes.h	18 Jun 2004 06:14:11 -0000	1.259
--- src/include/nodes/parsenodes.h	23 Jun 2004 04:40:55 -0000
***************
*** 1446,1451 ****
--- 1446,1468 ----
  } RenameStmt;
  
  /* ----------------------
+  *		Alter Object Owner To Statement 
+  * ----------------------
+  */
+ typedef struct OwnerStmt
+ {
+ 	NodeTag		type;
+ 	RangeVar   *relation;		/* in case it's a table */
+ 	List	   *object;			/* in case it's some other object */
+ 	List	   *objarg;			/* argument types, if applicable */
+ 	char	   *subname;		/* name of contained object (column, rule,
+ 								 * trigger, etc) */
+ 	char	   *newowner;		/* the new owner */
+ 	ObjectType	ownerType;		/* OBJECT_TABLE, OBJECT_COLUMN, etc */
+ } OwnerStmt;
+ 
+ 
+ /* ----------------------
   *		Create Rule Statement
   * ----------------------
   */
Index: src/test/regress/expected/conversion.out
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/conversion.out,v
retrieving revision 1.9
diff -c -r1.9 conversion.out
*** src/test/regress/expected/conversion.out	21 Nov 2003 22:32:49 -0000	1.9
--- src/test/regress/expected/conversion.out	23 Jun 2004 04:40:55 -0000
***************
*** 23,32 ****
  ERROR:  conversion "myconv_bad" does not exist
  COMMENT ON CONVERSION myconv IS 'bar';
  COMMENT ON CONVERSION myconv IS NULL;
  --
  -- drop user defined conversion
  --
! DROP CONVERSION myconv;
  DROP CONVERSION mydef;
  --
  -- make sure all pre-defined conversions are fine.
--- 23,39 ----
  ERROR:  conversion "myconv_bad" does not exist
  COMMENT ON CONVERSION myconv IS 'bar';
  COMMENT ON CONVERSION myconv IS NULL;
+ -- test owner and rename
+ RESET SESSION AUTHORIZATION;
+ CREATE USER regressuser1;
+ ALTER CONVERSION myconv RENAME TO new_myconv;
+ ALTER CONVERSION new_myconv OWNER TO regressuser1;
  --
  -- drop user defined conversion
  --
! DROP CONVERSION new_myconv;
! DROP USER regressuser1;
! SET SESSION AUTHORIZATION conversion_test_user;
  DROP CONVERSION mydef;
  --
  -- make sure all pre-defined conversions are fine.
Index: src/test/regress/expected/create_operator.out
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/create_operator.out,v
retrieving revision 1.6
diff -c -r1.6 create_operator.out
*** src/test/regress/expected/create_operator.out	1 Dec 2003 21:52:38 -0000	1.6
--- src/test/regress/expected/create_operator.out	23 Jun 2004 04:40:55 -0000
***************
*** 27,31 ****
--- 27,38 ----
     procedure = numeric_fac 
  );
  -- Test comments
+ COMMENT ON OPERATOR ## (path, path) IS 'operator comment';
  COMMENT ON OPERATOR ###### (int4, NONE) IS 'bad right unary';
  ERROR:  operator does not exist: integer ######
+ -- Test alter operator
+ CREATE USER regressuser1;
+ ALTER OPERATOR ## (path, path) OWNER TO regressuser1;
+ ALTER OPERATOR ###### (int4, NONE) OWNER TO regressuser1;
+ ERROR:  operator does not exist: integer ######
+ DROP USER regressuser1;
Index: src/test/regress/expected/create_type.out
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/create_type.out,v
retrieving revision 1.11
diff -c -r1.11 create_type.out
*** src/test/regress/expected/create_type.out	21 Nov 2003 22:32:49 -0000	1.11
--- src/test/regress/expected/create_type.out	23 Jun 2004 04:40:55 -0000
***************
*** 76,81 ****
--- 76,85 ----
  ERROR:  type "bad" does not exist
  COMMENT ON TYPE default_test_row IS 'good comment';
  COMMENT ON TYPE default_test_row IS NULL;
+ -- test owner
+ CREATE USER regressuser1;
+ ALTER TYPE default_test_row OWNER TO regressuser1;
  DROP TYPE default_test_row CASCADE;
  NOTICE:  drop cascades to function get_default_test()
+ DROP USER regressuser1;
  DROP TABLE default_test;
Index: src/test/regress/expected/namespace.out
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/namespace.out,v
retrieving revision 1.2
diff -c -r1.2 namespace.out
*** src/test/regress/expected/namespace.out	10 Jun 2004 17:56:01 -0000	1.2
--- src/test/regress/expected/namespace.out	23 Jun 2004 04:40:55 -0000
***************
*** 38,47 ****
   4 |  
  (3 rows)
  
! DROP SCHEMA test_schema_1 CASCADE;
! NOTICE:  drop cascades to view test_schema_1.abc_view
! NOTICE:  drop cascades to rule _RETURN on view test_schema_1.abc_view
! NOTICE:  drop cascades to table test_schema_1.abc
  -- verify that the objects were dropped
  SELECT COUNT(*) FROM pg_class WHERE relnamespace =
      (SELECT oid FROM pg_namespace WHERE nspname = 'test_schema_1');
--- 38,56 ----
   4 |  
  (3 rows)
  
! -- test rename / owner
! CREATE USER regressuser1;
! ALTER SCHEMA test_schema_1 RENAME TO test_schema_2;
! ALTER SCHEMA test_schema_2 OWNER TO regressuser1;
! -- test comments
! COMMENT ON SCHEMA test_schema_2 IS 'test schema comment';
! COMMENT ON SCHEMA bad IS 'will not work';
! ERROR:  schema "bad" does not exist
! DROP SCHEMA test_schema_2 CASCADE;
! NOTICE:  drop cascades to view test_schema_2.abc_view
! NOTICE:  drop cascades to rule _RETURN on view test_schema_2.abc_view
! NOTICE:  drop cascades to table test_schema_2.abc
! DROP USER regressuser1;
  -- verify that the objects were dropped
  SELECT COUNT(*) FROM pg_class WHERE relnamespace =
      (SELECT oid FROM pg_namespace WHERE nspname = 'test_schema_1');
Index: src/test/regress/input/create_function_2.source
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/input/create_function_2.source,v
retrieving revision 1.14
diff -c -r1.14 create_function_2.source
*** src/test/regress/input/create_function_2.source	14 Dec 2002 00:24:28 -0000	1.14
--- src/test/regress/input/create_function_2.source	23 Jun 2004 04:40:55 -0000
***************
*** 18,23 ****
--- 18,35 ----
     AS 'select person from hobbies_r where name = $1'
     LANGUAGE 'sql';
  
+ -- test rename and owner
+ CREATE FUNCTION dummy(hobbies_r.name%TYPE)
+    RETURNS hobbies_r.person%TYPE
+    AS 'select person from hobbies_r where name = $1'
+    LANGUAGE 'sql';
+ CREATE USER regressuser1;
+ ALTER FUNCTION dummy(hobbies_r.name%TYPE) RENAME TO dummy;
+ ALTER FUNCTION dummy(hobbies_r.name%TYPE) RENAME TO newdummy;
+ ALTER FUNCTION newdummy(hobbies_r.name%TYPE) OWNER TO bad;
+ ALTER FUNCTION newdummy(hobbies_r.name%TYPE) OWNER TO regressuser1;
+ DROP FUNCTION newdummy(hobbies_r.name%TYPE);
+ DROP USER regressuser1;
  
  CREATE FUNCTION equipment(hobbies_r)
     RETURNS setof equipment_r
Index: src/test/regress/input/tablespace.source
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/input/tablespace.source,v
retrieving revision 1.1
diff -c -r1.1 tablespace.source
*** src/test/regress/input/tablespace.source	18 Jun 2004 06:14:29 -0000	1.1
--- src/test/regress/input/tablespace.source	23 Jun 2004 04:40:55 -0000
***************
*** 27,36 ****
  -- No such tablespace
  CREATE TABLE bar (i int) TABLESPACE nosuchspace;
  
  -- Fail, not empty
! DROP TABLESPACE testspace;
  
  DROP SCHEMA testschema CASCADE;
  
  -- Should succeed
! DROP TABLESPACE testspace;
--- 27,44 ----
  -- No such tablespace
  CREATE TABLE bar (i int) TABLESPACE nosuchspace;
  
+ -- Rename and owner
+ CREATE USER regressuser1;
+ ALTER TABLESPACE testspace RENAME TO testspace;
+ ALTER TABLESPACE testspace RENAME TO newspace;
+ ALTER TABLESPACE newspace OWNER TO regressuser1;
+ ALTER TABLESPACE newspace OWNER TO baduser;
+ 
  -- Fail, not empty
! DROP TABLESPACE newspace;
  
  DROP SCHEMA testschema CASCADE;
  
  -- Should succeed
! DROP TABLESPACE newspace;
! DROP USER regressuser1;
Index: src/test/regress/sql/conversion.sql
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/sql/conversion.sql,v
retrieving revision 1.6
diff -c -r1.6 conversion.sql
*** src/test/regress/sql/conversion.sql	21 Nov 2003 22:32:49 -0000	1.6
--- src/test/regress/sql/conversion.sql	23 Jun 2004 04:40:55 -0000
***************
*** 20,29 ****
  COMMENT ON CONVERSION myconv_bad IS 'foo';
  COMMENT ON CONVERSION myconv IS 'bar';
  COMMENT ON CONVERSION myconv IS NULL;
  --
  -- drop user defined conversion
  --
! DROP CONVERSION myconv;
  DROP CONVERSION mydef;
  --
  -- make sure all pre-defined conversions are fine.
--- 20,36 ----
  COMMENT ON CONVERSION myconv_bad IS 'foo';
  COMMENT ON CONVERSION myconv IS 'bar';
  COMMENT ON CONVERSION myconv IS NULL;
+ -- test owner and rename
+ RESET SESSION AUTHORIZATION;
+ CREATE USER regressuser1;
+ ALTER CONVERSION myconv RENAME TO new_myconv;
+ ALTER CONVERSION new_myconv OWNER TO regressuser1;
  --
  -- drop user defined conversion
  --
! DROP CONVERSION new_myconv;
! DROP USER regressuser1;
! SET SESSION AUTHORIZATION conversion_test_user;
  DROP CONVERSION mydef;
  --
  -- make sure all pre-defined conversions are fine.
Index: src/test/regress/sql/create_operator.sql
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/sql/create_operator.sql,v
retrieving revision 1.6
diff -c -r1.6 create_operator.sql
*** src/test/regress/sql/create_operator.sql	1 Dec 2003 21:52:38 -0000	1.6
--- src/test/regress/sql/create_operator.sql	23 Jun 2004 04:40:55 -0000
***************
*** 33,38 ****
--- 33,44 ----
  );
  
  -- Test comments
+ COMMENT ON OPERATOR ## (path, path) IS 'operator comment';
  COMMENT ON OPERATOR ###### (int4, NONE) IS 'bad right unary';
  
+ -- Test alter operator
+ CREATE USER regressuser1;
+ ALTER OPERATOR ## (path, path) OWNER TO regressuser1;
+ ALTER OPERATOR ###### (int4, NONE) OWNER TO regressuser1;
+ DROP USER regressuser1;
  
Index: src/test/regress/sql/create_type.sql
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/sql/create_type.sql,v
retrieving revision 1.7
diff -c -r1.7 create_type.sql
*** src/test/regress/sql/create_type.sql	21 Nov 2003 22:32:49 -0000	1.7
--- src/test/regress/sql/create_type.sql	23 Jun 2004 04:40:55 -0000
***************
*** 74,79 ****
--- 74,83 ----
  COMMENT ON TYPE default_test_row IS 'good comment';
  COMMENT ON TYPE default_test_row IS NULL;
  
+ -- test owner
+ CREATE USER regressuser1;
+ ALTER TYPE default_test_row OWNER TO regressuser1;
  DROP TYPE default_test_row CASCADE;
+ DROP USER regressuser1;
  
  DROP TABLE default_test;
Index: src/test/regress/sql/namespace.sql
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/sql/namespace.sql,v
retrieving revision 1.1
diff -c -r1.1 namespace.sql
*** src/test/regress/sql/namespace.sql	11 Jan 2004 04:58:17 -0000	1.1
--- src/test/regress/sql/namespace.sql	23 Jun 2004 04:40:55 -0000
***************
*** 24,30 ****
  SELECT * FROM test_schema_1.abc;
  SELECT * FROM test_schema_1.abc_view;
  
! DROP SCHEMA test_schema_1 CASCADE;
  
  -- verify that the objects were dropped
  SELECT COUNT(*) FROM pg_class WHERE relnamespace =
--- 24,40 ----
  SELECT * FROM test_schema_1.abc;
  SELECT * FROM test_schema_1.abc_view;
  
! -- test rename / owner
! CREATE USER regressuser1;
! ALTER SCHEMA test_schema_1 RENAME TO test_schema_2;
! ALTER SCHEMA test_schema_2 OWNER TO regressuser1;
! 
! -- test comments
! COMMENT ON SCHEMA test_schema_2 IS 'test schema comment';
! COMMENT ON SCHEMA bad IS 'will not work';
! 
! DROP SCHEMA test_schema_2 CASCADE;
! DROP USER regressuser1;
  
  -- verify that the objects were dropped
  SELECT COUNT(*) FROM pg_class WHERE relnamespace =
