Re: ALTER TYPE RENAME

Поиск
Список
Период
Сортировка
Искать
От
Bruce Momjian
Тема
Re: ALTER TYPE RENAME
Дата
Msg-id
200711042154.lA4LsQa29775@momjian.us
Ответ на
ALTER TYPE RENAME (Petr Jelinek)
Список
Дерево обсуждения
ALTER TYPE RENAME Petr Jelinek <pjmodos@pjmodos.net>
Re: ALTER TYPE RENAME Bruce Momjian <bruce@momjian.us>
Re: ALTER TYPE RENAME Tom Lane <tgl@sss.pgh.pa.us>
Re: ALTER TYPE RENAME Tom Lane <tgl@sss.pgh.pa.us>
Re: ALTER TYPE RENAME Tom Lane <tgl@sss.pgh.pa.us>
Re: ALTER TYPE RENAME Petr Jelinek <pjmodos@pjmodos.net>
Re: ALTER TYPE RENAME Tom Lane <tgl@sss.pgh.pa.us>
Re: ALTER TYPE RENAME Petr Jelinek <pjmodos@pjmodos.net>
Re: ALTER TYPE RENAME Tom Lane <tgl@sss.pgh.pa.us>
Re: ALTER TYPE RENAME Bruce Momjian <bruce@momjian.us>

This has been saved for the 8.4 release:

	http://momjian.postgresql.org/cgi-bin/pgpatches_hold

---------------------------------------------------------------------------

Petr Jelinek wrote:
> Hi,
> I noticed we don't have ALTER TYPE foo RENAME TO bar command which would 
> be handy for me especially for enum types.
> So I wrote this little patch (including very brief doc) which adds above 
> syntax. It basically just does some checks and calls existing TypeRename 
> function which is used for renaming table rowtype now.
> I hope I haven't missed anything, but I am unsure about two things. 
> First, this patch allows renaming base types which I don't know if it's 
> desired. And second we might want to throw error when renaming rowtype 
> (there is check in AlterTypeOwner for this but not in AlterTypeNamespace 
> so I don't know).
> 
> -- 
> Regards
> Petr Jelinek (PJMODOS)
> 

> Index: doc/src/sgml/ref/alter_type.sgml
> ===================================================================
> RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ref/alter_type.sgml,v
> retrieving revision 1.4
> diff -c -r1.4 alter_type.sgml
> *** doc/src/sgml/ref/alter_type.sgml	16 Sep 2006 00:30:16 -0000	1.4
> --- doc/src/sgml/ref/alter_type.sgml	29 Sep 2007 05:43:14 -0000
> ***************
> *** 26,31 ****
> --- 26,32 ----
>     
>   ALTER TYPE name OWNER TO new_owner 
>   ALTER TYPE name SET SCHEMA new_schema
> + ALTER TYPE name RENAME TO new_name
>     
>    
>   
> ***************
> *** 83,88 ****
> --- 84,98 ----
>         
>        
>   
> +      
> +       new_name
> +       
> +        
> +         The new name for the type.
> +        
> +       
> +      
> + 
>       
>      
>     
> Index: src/backend/commands/alter.c
> ===================================================================
> RCS file: /projects/cvsroot/pgsql/src/backend/commands/alter.c,v
> retrieving revision 1.25
> diff -c -r1.25 alter.c
> *** src/backend/commands/alter.c	21 Aug 2007 01:11:14 -0000	1.25
> --- src/backend/commands/alter.c	29 Sep 2007 05:12:31 -0000
> ***************
> *** 154,159 ****
> --- 154,164 ----
>   			RenameTSConfiguration(stmt->object, stmt->newname);
>   			break;
>   
> + 		case OBJECT_TYPE:
> + 		case OBJECT_DOMAIN:
> + 			RenameType(stmt->object, stmt->newname);
> + 			break;
> + 
>   		default:
>   			elog(ERROR, "unrecognized rename stmt type: %d",
>   				 (int) stmt->renameType);
> Index: src/backend/commands/typecmds.c
> ===================================================================
> RCS file: /projects/cvsroot/pgsql/src/backend/commands/typecmds.c,v
> retrieving revision 1.107
> diff -c -r1.107 typecmds.c
> *** src/backend/commands/typecmds.c	4 Sep 2007 16:41:42 -0000	1.107
> --- src/backend/commands/typecmds.c	29 Sep 2007 05:11:22 -0000
> ***************
> *** 2514,2519 ****
> --- 2514,2567 ----
>   }
>   
>   /*
> +  * Execute ALTER TYPE RENAME
> +  */
> + void
> + RenameType(List *names, const char *newTypeName)
> + {
> + 	TypeName   *typename;
> + 	Oid			typeOid;
> + 	Relation	rel;
> + 	HeapTuple	tup;
> + 	Form_pg_type typTup;
> + 
> + 	/* Make a TypeName so we can use standard type lookup machinery */
> + 	typename = makeTypeNameFromNameList(names);
> + 	typeOid = typenameTypeId(NULL, typename);
> + 
> + 	/* Look up the type in the type table */
> + 	rel = heap_open(TypeRelationId, RowExclusiveLock);
> + 
> + 	tup = SearchSysCacheCopy(TYPEOID,
> + 							 ObjectIdGetDatum(typeOid),
> + 							 0, 0, 0);
> + 	if (!HeapTupleIsValid(tup))
> + 		elog(ERROR, "cache lookup failed for type %u", typeOid);
> + 	typTup = (Form_pg_type) GETSTRUCT(tup);
> + 
> + 	/* check permissions on type */
> + 	if (!pg_type_ownercheck(typeOid, GetUserId()))
> + 		aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE,
> + 					   format_type_be(typeOid));
> + 
> + 	/* don't allow direct alteration of array types */
> + 	if (OidIsValid(typTup->typelem) &&
> + 		get_array_type(typTup->typelem) == typeOid)
> + 		ereport(ERROR,
> + 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
> + 				 errmsg("cannot alter array type %s",
> + 						format_type_be(typeOid)),
> + 				 errhint("You can alter type %s, which will alter the array type as well.",
> + 						 format_type_be(typTup->typelem))));
> + 
> + 	/* and do the work */
> + 	TypeRename(typeOid, newTypeName, typTup->typnamespace);
> + 
> + 	/* Clean up */
> + 	heap_close(rel, RowExclusiveLock);
> + }
> + 
> + /*
>    * Move specified type to new namespace.
>    *
>    * Caller must have already checked privileges.
> Index: src/backend/parser/gram.y
> ===================================================================
> RCS file: /projects/cvsroot/pgsql/src/backend/parser/gram.y,v
> retrieving revision 2.603
> diff -c -r2.603 gram.y
> *** src/backend/parser/gram.y	24 Sep 2007 01:29:28 -0000	2.603
> --- src/backend/parser/gram.y	29 Sep 2007 05:13:21 -0000
> ***************
> *** 4748,4753 ****
> --- 4748,4761 ----
>   					n->newname = $8;
>   					$$ = (Node *)n;
>   				}
> + 			| ALTER TYPE_P any_name RENAME TO name
> + 				{
> + 					RenameStmt *n = makeNode(RenameStmt);
> + 					n->renameType = OBJECT_TYPE;
> + 					n->object = $3;
> + 					n->newname = $6;
> + 					$$ = (Node *)n;
> + 				}
>   		;
>   
>   opt_column: COLUMN									{ $$ = COLUMN; }
> Index: src/include/commands/typecmds.h
> ===================================================================
> RCS file: /projects/cvsroot/pgsql/src/include/commands/typecmds.h,v
> retrieving revision 1.19
> diff -c -r1.19 typecmds.h
> *** src/include/commands/typecmds.h	11 May 2007 17:57:14 -0000	1.19
> --- src/include/commands/typecmds.h	29 Sep 2007 05:11:57 -0000
> ***************
> *** 43,46 ****
> --- 43,48 ----
>   									   bool isImplicitArray,
>   									   bool errorOnTableType);
>   
> + extern void RenameType(List *names, const char *newTypeName);
> + 
>   #endif   /* TYPECMDS_H */

> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster

-- 
  Bruce Momjian          http://momjian.us
  EnterpriseDB                             http://postgres.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
В списке pgsql-patches по дате отправления
От: Andrew Dunstan
Дата:
От: Bruce Momjian
Дата:
FAQ