1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-18 17:42:25 +03:00

Rearrange ALTER TABLE syntax processing as per my recent proposal: the

grammar allows ALTER TABLE/INDEX/SEQUENCE/VIEW interchangeably for all
subforms of those commands, and then we sort out what's really legal
at execution time.  This allows the ALTER SEQUENCE/VIEW reference pages
to fully document all the ALTER forms available for sequences and views
respectively, and eliminates a longstanding cause of confusion for users.

The net effect is that the following forms are allowed that weren't before:
	ALTER SEQUENCE OWNER TO
	ALTER VIEW ALTER COLUMN SET/DROP DEFAULT
	ALTER VIEW OWNER TO
	ALTER VIEW SET SCHEMA
(There's no actual functionality gain here, but formerly you had to say
ALTER TABLE instead.)

Interestingly, the grammar tables actually get smaller, probably because
there are fewer special cases to keep track of.

I did not disallow using ALTER TABLE for these operations.  Perhaps we
should, but there's a backwards-compatibility issue if we do; in fact
it would break existing pg_dump scripts.  I did however tighten up
ALTER SEQUENCE and ALTER VIEW to reject non-sequences and non-views
in the new cases as well as a couple of cases where they didn't before.

The patch doesn't change pg_dump to use the new syntaxes, either.
This commit is contained in:
Tom Lane
2008-06-15 01:25:54 +00:00
parent bd2ef8707f
commit a0b012a1ab
7 changed files with 235 additions and 81 deletions

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_sequence.sgml,v 1.21 2008/05/17 01:20:39 tgl Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_sequence.sgml,v 1.22 2008/06/15 01:25:53 tgl Exp $
PostgreSQL documentation
-->
@ -30,6 +30,7 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> [ INCREMENT [ B
[ RESTART [ [ WITH ] <replaceable class="parameter">restart</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> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
ALTER SEQUENCE <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
</synopsis>
@ -48,6 +49,11 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
You must own the sequence to use <command>ALTER SEQUENCE</>.
To change a sequence's schema, you must also have <literal>CREATE</>
privilege on the new schema.
To alter the owner, you must also be a direct or indirect member of the new
owning role, and that role must have <literal>CREATE</literal> privilege on
the sequence's schema. (These restrictions enforce that altering the owner
doesn't do anything you couldn't do by dropping and recreating the sequence.
However, a superuser can alter ownership of any sequence anyway.)
</para>
</refsect1>
@ -205,6 +211,15 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
<listitem>
<para>
The user name of the new owner of the sequence.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">new_name</replaceable></term>
<listitem>
@ -233,9 +248,9 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
<para>
To avoid blocking of concurrent transactions that obtain numbers from the
same sequence, <command>ALTER SEQUENCE</command>'s effects on the sequence
generation parameters are never rolled back;
those changes take effect immediately and are not reversible. However,
the <literal>OWNED BY</>, <literal>RENAME</>, and <literal>SET SCHEMA</>
generation parameters are never rolled back; those changes take effect
immediately and are not reversible. However, the <literal>OWNED BY</>,
<literal>OWNER TO</>, <literal>RENAME TO</>, and <literal>SET SCHEMA</>
clauses cause ordinary catalog updates that can be rolled back.
</para>
@ -255,9 +270,9 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
</para>
<para>
Some variants of <command>ALTER TABLE</command> can be used with
sequences as well; for example, to rename a sequence it is also
possible to use <command>ALTER TABLE RENAME</command>.
For historical reasons, <command>ALTER TABLE</command> can be used with
sequences too; but the only variants of <command>ALTER TABLE</command>
that are allowed with sequences are equivalent to the forms shown above.
</para>
</refsect1>
@ -278,7 +293,7 @@ ALTER SEQUENCE serial RESTART WITH 105;
<para>
<command>ALTER SEQUENCE</command> conforms to the <acronym>SQL</acronym>
standard, except for the <literal>START WITH</>,
<literal>OWNED BY</>, <literal>RENAME</>, and
<literal>OWNED BY</>, <literal>OWNER TO</>, <literal>RENAME TO</>, and
<literal>SET SCHEMA</literal> clauses, which are
<productname>PostgreSQL</productname> extensions.
</para>

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_view.sgml,v 1.3 2007/10/03 16:48:43 tgl Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_view.sgml,v 1.4 2008/06/15 01:25:53 tgl Exp $
PostgreSQL documentation
-->
@ -20,7 +20,11 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
ALTER VIEW <replaceable>name</replaceable> RENAME TO <replaceable>newname</replaceable>
ALTER VIEW <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
ALTER VIEW <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
ALTER VIEW <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
ALTER VIEW <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
ALTER VIEW <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
</synopsis>
</refsynopsisdiv>
@ -28,9 +32,20 @@ ALTER VIEW <replaceable>name</replaceable> RENAME TO <replaceable>newname</repla
<title>Description</title>
<para>
<command>ALTER VIEW</command> changes the definition of a view.
The only currently available functionality is to rename the view.
To execute this command you must be the owner of the view.
<command>ALTER VIEW</command> changes various auxiliary properties
of a view. (If you want to modify the view's defining query,
use <command>CREATE OR REPLACE VIEW</>.)
</para>
<para>
You must own the view to use <command>ALTER VIEW</>.
To change a view's schema, you must also have <literal>CREATE</>
privilege on the new schema.
To alter the owner, you must also be a direct or indirect member of the new
owning role, and that role must have <literal>CREATE</literal> privilege on
the view's schema. (These restrictions enforce that altering the owner
doesn't do anything you couldn't do by dropping and recreating the view.
However, a superuser can alter ownership of any view anyway.)
</para>
</refsect1>
@ -48,10 +63,41 @@ ALTER VIEW <replaceable>name</replaceable> RENAME TO <replaceable>newname</repla
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">newname</replaceable></term>
<term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
<listitem>
<para>
The new name of the view.
These forms set or remove the default value for a column.
A default value associated with a view column is
inserted into <command>INSERT</> statements on the view before
the view's <literal>ON INSERT</literal> rule is applied, if
the <command>INSERT</> does not specify a value for the column.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
<listitem>
<para>
The user name of the new owner of the view.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">new_name</replaceable></term>
<listitem>
<para>
The new name for the view.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">new_schema</replaceable></term>
<listitem>
<para>
The new schema for the view.
</para>
</listitem>
</varlistentry>
@ -62,11 +108,9 @@ ALTER VIEW <replaceable>name</replaceable> RENAME TO <replaceable>newname</repla
<title>Notes</title>
<para>
Some variants of <command>ALTER TABLE</command> can be used with
views as well; for example, to rename a view it is also
possible to use <command>ALTER TABLE RENAME</command>. To change
the schema or owner of a view, you currently must use <command>ALTER
TABLE</>.
For historical reasons, <command>ALTER TABLE</command> can be used with
views too; but the only variants of <command>ALTER TABLE</command>
that are allowed with views are equivalent to the ones shown above.
</para>
</refsect1>