]> granicus.if.org Git - postgresql/commitdiff
Add ALTER VIEW ... RENAME TO, and a RENAME TO clause to ALTER SEQUENCE.
authorNeil Conway <neilc@samurai.com>
Tue, 3 Jul 2007 01:30:37 +0000 (01:30 +0000)
committerNeil Conway <neilc@samurai.com>
Tue, 3 Jul 2007 01:30:37 +0000 (01:30 +0000)
Sequences and views could previously be renamed using ALTER TABLE, but
this was a repeated source of confusion for users. Update the docs,
and psql tab completion. Patch from David Fetter; various minor fixes
by myself.

12 files changed:
doc/src/sgml/ref/allfiles.sgml
doc/src/sgml/ref/alter_sequence.sgml
doc/src/sgml/ref/alter_view.sgml [new file with mode: 0644]
doc/src/sgml/ref/create_view.sgml
doc/src/sgml/ref/drop_view.sgml
doc/src/sgml/reference.sgml
src/backend/commands/alter.c
src/backend/commands/tablecmds.c
src/backend/parser/gram.y
src/backend/tcop/utility.c
src/bin/psql/tab-complete.c
src/include/commands/tablecmds.h

index 90ad9adc53c3fe1e051bf6ed28bf773ea3192154..3e08b55bab12a1f3b88347bfbcdad821cc675bca 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/allfiles.sgml,v 1.70 2007/04/26 16:13:08 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/allfiles.sgml,v 1.71 2007/07/03 01:30:35 neilc Exp $
 PostgreSQL documentation
 Complete list of usable sgml source files in this directory.
 -->
@@ -25,6 +25,7 @@ Complete list of usable sgml source files in this directory.
 <!entity alterTrigger       system "alter_trigger.sgml">
 <!entity alterType          system "alter_type.sgml">
 <!entity alterUser          system "alter_user.sgml">
+<!entity alterView          system "alter_view.sgml">
 <!entity analyze            system "analyze.sgml">
 <!entity begin              system "begin.sgml">
 <!entity checkpoint         system "checkpoint.sgml">
index 37c2ceb6314c8db13a6c39473fd4be47dd354727..c2bc3cd8bf516479603f50bd88af448525348f24 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/alter_sequence.sgml,v 1.16 2007/01/31 23:26:02 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/alter_sequence.sgml,v 1.17 2007/07/03 01:30:35 neilc Exp $
 PostgreSQL documentation
 -->
 
@@ -29,6 +29,7 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> [ INCREMENT [ B
     [ RESTART [ WITH ] <replaceable class="parameter">start</replaceable> ] [ CACHE <replaceable class="parameter">cache</replaceable> ] [ [ NO ] CYCLE ]
     [ OWNED BY { <replaceable class="parameter">table</replaceable>.<replaceable class="parameter">column</replaceable> | NONE } ]
 ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
+ALTER SEQUENCE <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
   </synopsis>
  </refsynopsisdiv>
 
@@ -182,14 +183,24 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
     </listitem>
    </varlistentry>
 
-     <varlistentry>
-      <term><replaceable class="parameter">new_schema</replaceable></term>
-      <listitem>
-       <para>
-        The new schema for the sequence.
-       </para>
-      </listitem>
-     </varlistentry>
+   <varlistentry>
+    <term><replaceable class="parameter">new_schema</replaceable></term>
+    <listitem>
+     <para>
+      The new schema for the sequence.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><replaceable class="parameter">new_name</replaceable></term>
+    <listitem>
+     <para>
+      The new name for the sequence.
+     </para>
+    </listitem>
+   </varlistentry>
+
     </variablelist>
    </para>
   </refsect1>
diff --git a/doc/src/sgml/ref/alter_view.sgml b/doc/src/sgml/ref/alter_view.sgml
new file mode 100644 (file)
index 0000000..62723c7
--- /dev/null
@@ -0,0 +1,88 @@
+<!--
+$PostgreSQL: pgsql/doc/src/sgml/ref/alter_view.sgml,v 1.1 2007/07/03 01:30:35 neilc Exp $
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-ALTERVIEW">
+ <refmeta>
+  <refentrytitle id="SQL-ALTERVIEW-TITLE">ALTER VIEW</refentrytitle>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>ALTER VIEW</refname>
+  <refpurpose>change the definition of a view</refpurpose>
+ </refnamediv>  
+  
+ <indexterm zone="sql-alterview">
+  <primary>ALTER VIEW</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+<synopsis>
+ALTER VIEW <replaceable>name</replaceable> RENAME TO <replaceable>newname</replaceable>
+</synopsis>
+ </refsynopsisdiv>
+  
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <command>ALTER VIEW</command> changes the definition of a
+   view. To execute this command you must be the owner of the view.
+  </para>
+ </refsect1>
+  
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><replaceable class="parameter">name</replaceable></term>
+    <listitem>
+     <para>
+      The name (optionally schema-qualified) of an existing view.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><replaceable class="parameter">newname</replaceable></term>
+    <listitem>
+     <para>
+      The new name of the view.
+     </para>
+    </listitem>
+   </varlistentry>
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   To rename the view <literal>foo</literal> to
+   <literal>bar</literal>:
+<programlisting>
+ALTER VIEW foo RENAME TO bar;
+</programlisting>
+  </para>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>ALTER VIEW</command> is a <productname>PostgreSQL</>
+   extension of the SQL standard.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-createview" endterm="sql-createview-title"></member>
+   <member><xref linkend="sql-dropview" endterm="sql-dropview-title"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
index f1466c867800acb99c922775aab64eb6302a9196..1ad50f05d2ef80735f5e2436b33eef26d46ef19f 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.35 2007/02/01 00:28:18 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.36 2007/07/03 01:30:35 neilc Exp $
 PostgreSQL documentation
 -->
 
@@ -226,6 +226,7 @@ CREATE VIEW <replaceable class="parameter">name</replaceable> [ ( <replaceable c
   <title>See Also</title>
 
   <simplelist type="inline">
+   <member><xref linkend="sql-alterview" endterm="sql-alterview-title"></member>
    <member><xref linkend="sql-dropview" endterm="sql-dropview-title"></member>
   </simplelist>
  </refsect1>
index 03a8d4aa0e2d7a2257ed24810f95e00a64a36d96..b531c6d235b20cbb02182111c92be069f8088acd 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/drop_view.sgml,v 1.23 2006/09/16 00:30:18 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/drop_view.sgml,v 1.24 2007/07/03 01:30:35 neilc Exp $
 PostgreSQL documentation
 -->
 
@@ -104,6 +104,7 @@ DROP VIEW kinds;
   <title>See Also</title>
 
   <simplelist type="inline">
+   <member><xref linkend="sql-alterview" endterm="sql-alterview-title"></member>
    <member><xref linkend="sql-createview" endterm="sql-createview-title"></member>
   </simplelist>
  </refsect1>
index 06699a5b34ba920d8afe4d3c424a650d91e697f2..a52fe7a9644b6c12ddba13d48bc52c6ec1ce10bc 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/reference.sgml,v 1.63 2007/04/26 16:13:08 neilc Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/reference.sgml,v 1.64 2007/07/03 01:30:35 neilc Exp $ -->
 
 <part id="reference">
  <title>Reference</title>
@@ -53,6 +53,7 @@
    &alterTrigger;
    &alterType;
    &alterUser;
+   &alterView;
    &analyze;
    &begin;
    &checkpoint;
index 118e4adb16650ea86088c46468ce97bdae03c1c1..7cd347b69f99fcec98e16df4025669df3290c950 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.23 2007/03/26 16:58:38 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.24 2007/07/03 01:30:36 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -83,6 +83,8 @@ ExecRenameStmt(RenameStmt *stmt)
                        break;
 
                case OBJECT_TABLE:
+               case OBJECT_SEQUENCE:
+               case OBJECT_VIEW:
                case OBJECT_INDEX:
                case OBJECT_COLUMN:
                case OBJECT_TRIGGER:
@@ -96,6 +98,8 @@ ExecRenameStmt(RenameStmt *stmt)
                                switch (stmt->renameType)
                                {
                                        case OBJECT_TABLE:
+                                       case OBJECT_SEQUENCE:
+                                       case OBJECT_VIEW:
                                        case OBJECT_INDEX:
                                                {
                                                        /*
@@ -113,7 +117,7 @@ ExecRenameStmt(RenameStmt *stmt)
                                                                aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
                                                                                        get_namespace_name(namespaceId));
 
-                                                       renamerel(relid, stmt->newname);
+                                                       renamerel(relid, stmt->newname, stmt->renameType);
                                                        break;
                                                }
                                        case OBJECT_COLUMN:
index f50b59d0d8fa8dab7d4d9886fdc9f997c49c94cc..4bc2a25fcddde07c275e10c50f4cddcd7097c543 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.228 2007/06/23 22:12:50 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.229 2007/07/03 01:30:36 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -41,6 +41,7 @@
 #include "executor/executor.h"
 #include "miscadmin.h"
 #include "nodes/makefuncs.h"
+#include "nodes/parsenodes.h"
 #include "optimizer/clauses.h"
 #include "optimizer/plancat.h"
 #include "optimizer/prep.h"
@@ -1621,7 +1622,7 @@ renameatt(Oid myrelid,
  *                       sequence, AFAIK there's no need for it to be there.
  */
 void
-renamerel(Oid myrelid, const char *newrelname)
+renamerel(Oid myrelid, const char *newrelname, ObjectType reltype)
 {
        Relation        targetrelation;
        Relation        relrelation;    /* for RELATION relation */
@@ -1633,8 +1634,8 @@ renamerel(Oid myrelid, const char *newrelname)
        bool            relhastriggers;
 
        /*
-        * Grab an exclusive lock on the target table or index, which we will NOT
-        * release until end of transaction.
+        * Grab an exclusive lock on the target table, index, sequence or
+        * view, which we will NOT release until end of transaction.
         */
        targetrelation = relation_open(myrelid, AccessExclusiveLock);
 
@@ -1647,7 +1648,24 @@ renamerel(Oid myrelid, const char *newrelname)
                                 errmsg("permission denied: \"%s\" is a system catalog",
                                                RelationGetRelationName(targetrelation))));
 
+       /*
+        * For compatibility with prior releases, we don't complain if
+        * ALTER TABLE or ALTER INDEX is used to rename a sequence or
+        * view.
+        */
        relkind = targetrelation->rd_rel->relkind;
+       if (reltype == OBJECT_SEQUENCE && relkind != 'S')
+               ereport(ERROR,
+                               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                                errmsg("\"%s\" is not a sequence",
+                                               RelationGetRelationName(targetrelation))));
+
+       if (reltype == OBJECT_VIEW && relkind != 'v')
+               ereport(ERROR,
+                               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                                errmsg("\"%s\" is not a view",
+                                               RelationGetRelationName(targetrelation))));
+
        relhastriggers = (targetrelation->rd_rel->reltriggers > 0);
 
        /*
index d9e981bb5724b99440c4945e7606e3132048bb64..324f89a256634c3988b7596f27f8390b6cefacbf 100644 (file)
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.596 2007/06/23 22:12:51 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.597 2007/07/03 01:30:36 neilc Exp $
  *
  * HISTORY
  *       AUTHOR                        DATE                    MAJOR EVENT
@@ -4546,6 +4546,24 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->newname = $6;
                                        $$ = (Node *)n;
                                }
+                       | ALTER SEQUENCE relation_expr RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_SEQUENCE;
+                                       n->relation = $3;
+                                       n->subname = NULL;
+                                       n->newname = $6;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER VIEW relation_expr RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_VIEW;
+                                       n->relation = $3;
+                                       n->subname = NULL;
+                                       n->newname = $6;
+                                       $$ = (Node *)n;
+                               }
                        | ALTER INDEX relation_expr RENAME TO name
                                {
                                        RenameStmt *n = makeNode(RenameStmt);
index a01c99b005b35aae6c41bf35f3aa7e7c991b248a..ec9aa9d26374514b8837896396090c0a83674b2a 100644 (file)
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.282 2007/06/28 00:02:39 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.283 2007/07/03 01:30:37 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1574,6 +1574,9 @@ CreateCommandTag(Node *parsetree)
                                case OBJECT_SCHEMA:
                                        tag = "ALTER SCHEMA";
                                        break;
+                               case OBJECT_SEQUENCE:
+                                       tag = "ALTER SEQUENCE";
+                                       break;
                                case OBJECT_COLUMN:
                                case OBJECT_TABLE:
                                        tag = "ALTER TABLE";
@@ -1584,6 +1587,9 @@ CreateCommandTag(Node *parsetree)
                                case OBJECT_TRIGGER:
                                        tag = "ALTER TRIGGER";
                                        break;
+                               case OBJECT_VIEW:
+                                       tag = "ALTER VIEW";
+                                       break;
                                default:
                                        tag = "???";
                                        break;
index a30a07501fad6408b38715a1b132d1280645c1cc..9a9e952ac96c8b5761538fb8dda606656f7ae9ab 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.165 2007/06/13 23:59:47 neilc Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.166 2007/07/03 01:30:37 neilc Exp $
  */
 
 /*----------------------------------------------------------------------
@@ -602,7 +602,7 @@ psql_completion(char *text, int start, int end)
                static const char *const list_ALTER[] =
                {"AGGREGATE", "CONVERSION", "DATABASE", "DOMAIN", "FUNCTION",
                        "GROUP", "INDEX", "LANGUAGE", "OPERATOR", "ROLE", "SCHEMA", "SEQUENCE", "TABLE",
-               "TABLESPACE", "TRIGGER", "TYPE", "USER", NULL};
+               "TABLESPACE", "TRIGGER", "TYPE", "USER", "VIEW", NULL};
 
                COMPLETE_WITH_LIST(list_ALTER);
        }
@@ -714,7 +714,7 @@ psql_completion(char *text, int start, int end)
        {
                static const char *const list_ALTERSEQUENCE[] =
                {"INCREMENT", "MINVALUE", "MAXVALUE", "RESTART", "NO", "CACHE", "CYCLE",
-               "SET SCHEMA", NULL};
+               "SET SCHEMA", "RENAME TO", NULL};
 
                COMPLETE_WITH_LIST(list_ALTERSEQUENCE);
        }
@@ -728,6 +728,14 @@ psql_completion(char *text, int start, int end)
 
                COMPLETE_WITH_LIST(list_ALTERSEQUENCE2);
        }
+       /* ALTER VIEW <name> */
+       else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
+                        pg_strcasecmp(prev2_wd, "VIEW") == 0)
+       {
+               static const char *const list_ALTERVIEW[] = {"RENAME TO", NULL};
+
+               COMPLETE_WITH_LIST(list_ALTERVIEW);
+       }
        /* ALTER TRIGGER <name>, add ON */
        else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
                         pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
index 3acbe84b522cef18de802bf602985104cad2368f..44df10ceb3acbfb494d6f5422f83fc0b5f2dee47 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/commands/tablecmds.h,v 1.33 2007/05/11 20:17:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/commands/tablecmds.h,v 1.34 2007/07/03 01:30:37 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -43,7 +43,8 @@ extern void renameatt(Oid myrelid,
                  bool recursing);
 
 extern void renamerel(Oid myrelid,
-                 const char *newrelname);
+                 const char *newrelname,
+                 ObjectType reltype);
 
 extern void find_composite_type_dependencies(Oid typeOid,
                                                                                         const char *origTblName,