<!--
-$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
-->
[ 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>
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>
</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>
<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>
</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>
<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>
<!--
-$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
-->
<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>
<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>
</varlistentry>
<varlistentry>
- <term><replaceable class="parameter">newname</replaceable></term>
+ <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
+ <listitem>
+ <para>
+ 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 name of the view.
+ The new schema for the view.
</para>
</listitem>
</varlistentry>
<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>
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.28 2008/03/19 18:38:30 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/alter.c,v 1.29 2008/06/15 01:25:53 tgl Exp $
*
*-------------------------------------------------------------------------
*/
case OBJECT_SEQUENCE:
case OBJECT_TABLE:
+ case OBJECT_VIEW:
CheckRelationOwnership(stmt->relation, true);
- AlterTableNamespace(stmt->relation, stmt->newschema);
+ AlterTableNamespace(stmt->relation, stmt->newschema,
+ stmt->objectType);
break;
case OBJECT_TYPE:
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.256 2008/06/14 18:04:33 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.257 2008/06/15 01:25:53 tgl Exp $
*
*-------------------------------------------------------------------------
*/
CheckTableNotInUse(rel, "ALTER TABLE");
+ /* Check relation type against type specified in the ALTER command */
+ switch (stmt->relkind)
+ {
+ case OBJECT_TABLE:
+ /*
+ * For mostly-historical reasons, we allow ALTER TABLE to apply
+ * to all relation types.
+ */
+ break;
+
+ case OBJECT_INDEX:
+ if (rel->rd_rel->relkind != RELKIND_INDEX)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is not an index",
+ RelationGetRelationName(rel))));
+ break;
+
+ case OBJECT_SEQUENCE:
+ if (rel->rd_rel->relkind != RELKIND_SEQUENCE)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is not a sequence",
+ RelationGetRelationName(rel))));
+ break;
+
+ case OBJECT_VIEW:
+ if (rel->rd_rel->relkind != RELKIND_VIEW)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is not a view",
+ RelationGetRelationName(rel))));
+ break;
+
+ default:
+ elog(ERROR, "unrecognized object type: %d", (int) stmt->relkind);
+ }
+
ATController(rel, stmt->cmds, interpretInhOption(stmt->relation->inhOpt));
}
* Note: caller must have checked ownership of the relation already
*/
void
-AlterTableNamespace(RangeVar *relation, const char *newschema)
+AlterTableNamespace(RangeVar *relation, const char *newschema,
+ ObjectType stmttype)
{
Relation rel;
Oid relid;
relid = RelationGetRelid(rel);
oldNspOid = RelationGetNamespace(rel);
+ /* Check relation type against type specified in the ALTER command */
+ switch (stmttype)
+ {
+ case OBJECT_TABLE:
+ /*
+ * For mostly-historical reasons, we allow ALTER TABLE to apply
+ * to all relation types.
+ */
+ break;
+
+ case OBJECT_SEQUENCE:
+ if (rel->rd_rel->relkind != RELKIND_SEQUENCE)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is not a sequence",
+ RelationGetRelationName(rel))));
+ break;
+
+ case OBJECT_VIEW:
+ if (rel->rd_rel->relkind != RELKIND_VIEW)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is not a view",
+ RelationGetRelationName(rel))));
+ break;
+
+ default:
+ elog(ERROR, "unrecognized object type: %d", (int) stmttype);
+ }
+
/* Can we change the schema of this tuple? */
switch (rel->rd_rel->relkind)
{
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.615 2008/05/16 23:36:05 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.616 2008/06/15 01:25:54 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
%type <node> alter_column_default opclass_item opclass_drop alter_using
%type <ival> add_drop opt_asc_desc opt_nulls_order
-%type <node> alter_table_cmd alter_rel_cmd
-%type <list> alter_table_cmds alter_rel_cmds
+%type <node> alter_table_cmd
+%type <list> alter_table_cmds
%type <dbehavior> opt_drop_behavior
%type <node> fetch_direction select_limit_value select_offset_value
-%type <list> OptSeqList
-%type <defelt> OptSeqElem
+%type <list> OptSeqOptList SeqOptList
+%type <defelt> SeqOptElem
%type <istmt> insert_rest
/*****************************************************************************
*
- * ALTER [ TABLE | INDEX ] variations
+ * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
*
+ * Note: we accept all subcommands for each of the four variants, and sort
+ * out what's really legal at execution time.
*****************************************************************************/
AlterTableStmt:
n->relkind = OBJECT_TABLE;
$$ = (Node *)n;
}
- | ALTER INDEX relation_expr alter_rel_cmds
+ | ALTER INDEX relation_expr alter_table_cmds
{
AlterTableStmt *n = makeNode(AlterTableStmt);
n->relation = $3;
n->relkind = OBJECT_INDEX;
$$ = (Node *)n;
}
+ | ALTER SEQUENCE relation_expr alter_table_cmds
+ {
+ AlterTableStmt *n = makeNode(AlterTableStmt);
+ n->relation = $3;
+ n->cmds = $4;
+ n->relkind = OBJECT_SEQUENCE;
+ $$ = (Node *)n;
+ }
+ | ALTER VIEW relation_expr alter_table_cmds
+ {
+ AlterTableStmt *n = makeNode(AlterTableStmt);
+ n->relation = $3;
+ n->cmds = $4;
+ n->relkind = OBJECT_VIEW;
+ $$ = (Node *)n;
+ }
;
alter_table_cmds:
| alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
;
-/* Subcommands that are for ALTER TABLE only */
alter_table_cmd:
- /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
+ /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
ADD_P opt_column columnDef
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = $3;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
+ /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
| ALTER opt_column ColId alter_column_default
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = $4;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
+ /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
| ALTER opt_column ColId DROP NOT NULL_P
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->name = $3;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
+ /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
| ALTER opt_column ColId SET NOT NULL_P
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->name = $3;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
+ /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
| ALTER opt_column ColId SET STATISTICS IntegerOnly
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = (Node *) $6;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
+ /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
| ALTER opt_column ColId SET STORAGE ColId
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = (Node *) makeString($6);
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
+ /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
| DROP opt_column ColId opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
$$ = (Node *)n;
}
/*
- * ALTER TABLE <relation> ALTER [COLUMN] <colname> TYPE <typename>
+ * ALTER TABLE <name> ALTER [COLUMN] <colname> TYPE <typename>
* [ USING <expression> ]
*/
| ALTER opt_column ColId TYPE_P Typename alter_using
n->transform = $6;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> ADD CONSTRAINT ... */
+ /* ALTER TABLE <name> ADD CONSTRAINT ... */
| ADD_P TableConstraint
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = $2;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
+ /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
| DROP CONSTRAINT name opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->behavior = $4;
$$ = (Node *)n;
}
- /* ALTER TABLE <relation> SET WITHOUT OIDS */
+ /* ALTER TABLE <name> SET WITHOUT OIDS */
| SET WITHOUT OIDS
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = (Node *) $3;
$$ = (Node *)n;
}
- | alter_rel_cmd
- {
- $$ = $1;
- }
- ;
-
-alter_rel_cmds:
- alter_rel_cmd { $$ = list_make1($1); }
- | alter_rel_cmds ',' alter_rel_cmd { $$ = lappend($1, $3); }
- ;
-
-/* Subcommands that are for ALTER TABLE or ALTER INDEX */
-alter_rel_cmd:
- /* ALTER [TABLE|INDEX] <name> OWNER TO RoleId */
- OWNER TO RoleId
+ /* ALTER TABLE <name> OWNER TO RoleId */
+ | OWNER TO RoleId
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_ChangeOwner;
n->name = $3;
$$ = (Node *)n;
}
- /* ALTER [TABLE|INDEX] <name> SET TABLESPACE <tablespacename> */
+ /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
| SET TABLESPACE name
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->name = $3;
$$ = (Node *)n;
}
- /* ALTER [TABLE|INDEX] <name> SET (...) */
+ /* ALTER TABLE <name> SET (...) */
| SET definition
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->def = (Node *)$2;
$$ = (Node *)n;
}
- /* ALTER [TABLE|INDEX] <name> RESET (...) */
+ /* ALTER TABLE <name> RESET (...) */
| RESET definition
{
AlterTableCmd *n = makeNode(AlterTableCmd);
*****************************************************************************/
CreateSeqStmt:
- CREATE OptTemp SEQUENCE qualified_name OptSeqList
+ CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
{
CreateSeqStmt *n = makeNode(CreateSeqStmt);
$4->istemp = $2;
;
AlterSeqStmt:
- ALTER SEQUENCE qualified_name OptSeqList
+ ALTER SEQUENCE relation_expr SeqOptList
{
AlterSeqStmt *n = makeNode(AlterSeqStmt);
n->sequence = $3;
}
;
-OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
+OptSeqOptList: SeqOptList { $$ = $1; }
| /*EMPTY*/ { $$ = NIL; }
;
-OptSeqElem: CACHE NumericOnly
+SeqOptList: SeqOptElem { $$ = list_make1($1); }
+ | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
+ ;
+
+SeqOptElem: CACHE NumericOnly
{
$$ = makeDefElem("cache", (Node *)$2);
}
n->newschema = $6;
$$ = (Node *)n;
}
+ | ALTER TABLE relation_expr SET SCHEMA name
+ {
+ AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
+ n->objectType = OBJECT_TABLE;
+ n->relation = $3;
+ n->newschema = $6;
+ $$ = (Node *)n;
+ }
| ALTER SEQUENCE relation_expr SET SCHEMA name
{
AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
n->newschema = $6;
$$ = (Node *)n;
}
- | ALTER TABLE relation_expr SET SCHEMA name
+ | ALTER VIEW relation_expr SET SCHEMA name
{
AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
- n->objectType = OBJECT_TABLE;
+ n->objectType = OBJECT_VIEW;
n->relation = $3;
n->newschema = $6;
$$ = (Node *)n;
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.293 2008/06/14 18:04:33 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.294 2008/06/15 01:25:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
case OBJECT_TSCONFIGURATION:
tag = "ALTER TEXT SEARCH CONFIGURATION";
break;
+ case OBJECT_VIEW:
+ tag = "ALTER VIEW";
+ break;
default:
tag = "???";
break;
break;
case T_AlterTableStmt:
+ switch (((AlterTableStmt *) parsetree)->relkind)
{
- AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
-
- /*
- * We might be supporting ALTER INDEX here, so set the
- * completion tag appropriately. Catch all other possibilities
- * with ALTER TABLE
- */
-
- if (stmt->relkind == OBJECT_INDEX)
- tag = "ALTER INDEX";
- else
+ case OBJECT_TABLE:
tag = "ALTER TABLE";
+ break;
+ case OBJECT_INDEX:
+ tag = "ALTER INDEX";
+ break;
+ case OBJECT_SEQUENCE:
+ tag = "ALTER SEQUENCE";
+ break;
+ case OBJECT_VIEW:
+ tag = "ALTER VIEW";
+ break;
+ default:
+ tag = "???";
+ break;
}
break;
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/commands/tablecmds.h,v 1.39 2008/06/14 18:04:34 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/commands/tablecmds.h,v 1.40 2008/06/15 01:25:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
extern void AlterTableInternal(Oid relid, List *cmds, bool recurse);
-extern void AlterTableNamespace(RangeVar *relation, const char *newschema);
+extern void AlterTableNamespace(RangeVar *relation, const char *newschema,
+ ObjectType stmttype);
extern void AlterRelationNamespaceInternal(Relation classRel, Oid relOid,
Oid oldNspOid, Oid newNspOid,