Re: ALTER TYPE RENAME

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Petr Jelinek <pjmodos(at)pjmodos(dot)net>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: ALTER TYPE RENAME
Date: 2007-11-04 21:54:26
Message-ID: 200711042154.lA4LsQa29775@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches


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 ----
> <synopsis>
> ALTER TYPE <replaceable class="PARAMETER">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
> ALTER TYPE <replaceable class="PARAMETER">name</replaceable> SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
> + ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
> </synopsis>
> </refsynopsisdiv>
>
> ***************
> *** 83,88 ****
> --- 84,98 ----
> </listitem>
> </varlistentry>
>
> + <varlistentry>
> + <term><replaceable class="PARAMETER">new_name</replaceable></term>
> + <listitem>
> + <para>
> + The new name for the type.
> + </para>
> + </listitem>
> + </varlistentry>
> +
> </variablelist>
> </para>
> </refsect1>
> 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 <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://postgres.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

In response to

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2007-11-04 21:58:08 Re: [HACKERS] Text <-> C string
Previous Message Andrew Dunstan 2007-11-04 21:27:09 Re: [HACKERS] Unclarity of configure options