1999-07-22 17:09:15 +02:00
<!--
2010-09-20 22:08:53 +02:00
doc/src/sgml/ref/alter_table.sgml
2001-12-08 04:24:40 +01:00
PostgreSQL documentation
1999-07-22 17:09:15 +02:00
-->
1999-07-06 19:16:42 +02:00
<refentry id="SQL-ALTERTABLE">
2014-02-24 03:25:35 +01:00
<indexterm zone="sql-altertable">
<primary>ALTER TABLE</primary>
</indexterm>
1999-07-06 19:16:42 +02:00
<refmeta>
2010-04-03 09:23:02 +02:00
<refentrytitle>ALTER TABLE</refentrytitle>
2008-11-14 11:22:48 +01:00
<manvolnum>7</manvolnum>
1999-07-06 19:16:42 +02:00
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
2003-04-15 15:25:08 +02:00
1999-07-06 19:16:42 +02:00
<refnamediv>
2003-04-15 15:25:08 +02:00
<refname>ALTER TABLE</refname>
<refpurpose>change the definition of a table</refpurpose>
1999-07-06 19:16:42 +02:00
</refnamediv>
2003-04-15 15:25:08 +02:00
1999-07-06 19:16:42 +02:00
<refsynopsisdiv>
2003-04-15 15:25:08 +02:00
<synopsis>
2012-01-24 00:25:04 +01:00
ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
2004-05-05 06:48:48 +02:00
<replaceable class="PARAMETER">action</replaceable> [, ... ]
2012-01-24 00:25:04 +01:00
ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
2012-06-22 00:06:14 +02:00
RENAME [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> TO <replaceable class="PARAMETER">new_column_name</replaceable>
2012-03-10 19:19:13 +01:00
ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
RENAME CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> TO <replaceable class="PARAMETER">new_constraint_name</replaceable>
2012-01-24 00:25:04 +01:00
ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
2004-05-05 06:48:48 +02:00
RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
2012-01-24 00:25:04 +01:00
ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
2005-08-01 18:11:14 +02:00
SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
2014-08-22 01:06:17 +02:00
ALTER TABLE ALL IN TABLESPACE <replaceable class="PARAMETER">name</replaceable> [ OWNED BY <replaceable class="PARAMETER">role_name</replaceable> [, ... ] ]
SET TABLESPACE <replaceable class="PARAMETER">new_tablespace</replaceable> [ NOWAIT ]
2004-05-05 06:48:48 +02:00
2009-09-18 07:00:42 +02:00
<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
2004-05-05 06:48:48 +02:00
2012-06-22 00:06:14 +02:00
ADD [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ COLLATE <replaceable class="PARAMETER">collation</replaceable> ] [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
DROP [ COLUMN ] [ IF EXISTS ] <replaceable class="PARAMETER">column_name</replaceable> [ RESTRICT | CASCADE ]
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">data_type</replaceable> [ COLLATE <replaceable class="PARAMETER">collation</replaceable> ] [ USING <replaceable class="PARAMETER">expression</replaceable> ]
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> DROP DEFAULT
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> { SET | DROP } NOT NULL
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> SET ( <replaceable class="PARAMETER">attribute_option</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> RESET ( <replaceable class="PARAMETER">attribute_option</replaceable> [, ... ] )
ALTER [ COLUMN ] <replaceable class="PARAMETER">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
2011-02-08 13:23:20 +01:00
ADD <replaceable class="PARAMETER">table_constraint</replaceable> [ NOT VALID ]
2011-06-16 01:05:11 +02:00
ADD <replaceable class="PARAMETER">table_constraint_using_index</replaceable>
2013-06-29 01:27:30 +02:00
ALTER CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
2011-02-08 13:23:20 +01:00
VALIDATE CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable>
2009-07-20 04:42:28 +02:00
DROP CONSTRAINT [ IF EXISTS ] <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
2005-08-24 19:24:19 +02:00
DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
2007-03-20 00:38:32 +01:00
ENABLE REPLICA TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
ENABLE ALWAYS TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
DISABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
ENABLE RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
ENABLE REPLICA RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
ENABLE ALWAYS RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
Row-Level Security Policies (RLS)
Building on the updatable security-barrier views work, add the
ability to define policies on tables to limit the set of rows
which are returned from a query and which are allowed to be added
to a table. Expressions defined by the policy for filtering are
added to the security barrier quals of the query, while expressions
defined to check records being added to a table are added to the
with-check options of the query.
New top-level commands are CREATE/ALTER/DROP POLICY and are
controlled by the table owner. Row Security is able to be enabled
and disabled by the owner on a per-table basis using
ALTER TABLE .. ENABLE/DISABLE ROW SECURITY.
Per discussion, ROW SECURITY is disabled on tables by default and
must be enabled for policies on the table to be used. If no
policies exist on a table with ROW SECURITY enabled, a default-deny
policy is used and no records will be visible.
By default, row security is applied at all times except for the
table owner and the superuser. A new GUC, row_security, is added
which can be set to ON, OFF, or FORCE. When set to FORCE, row
security will be applied even for the table owner and superusers.
When set to OFF, row security will be disabled when allowed and an
error will be thrown if the user does not have rights to bypass row
security.
Per discussion, pg_dump sets row_security = OFF by default to ensure
that exports and backups will have all data in the table or will
error if there are insufficient privileges to bypass row security.
A new option has been added to pg_dump, --enable-row-security, to
ask pg_dump to export with row security enabled.
A new role capability, BYPASSRLS, which can only be set by the
superuser, is added to allow other users to be able to bypass row
security using row_security = OFF.
Many thanks to the various individuals who have helped with the
design, particularly Robert Haas for his feedback.
Authors include Craig Ringer, KaiGai Kohei, Adam Brightwell, Dean
Rasheed, with additional changes and rework by me.
Reviewers have included all of the above, Greg Smith,
Jeff McCormick, and Robert Haas.
2014-09-19 17:18:35 +02:00
DISABLE ROW LEVEL SECURITY
ENABLE ROW LEVEL SECURITY
2003-09-22 02:16:58 +02:00
CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
2004-06-02 23:01:10 +02:00
SET WITHOUT CLUSTER
2009-02-11 22:11:16 +01:00
SET WITH OIDS
2004-07-12 01:13:58 +02:00
SET WITHOUT OIDS
2014-08-22 20:27:00 +02:00
SET TABLESPACE <replaceable class="PARAMETER">new_tablespace</replaceable>
2015-05-22 14:42:15 +02:00
SET { LOGGED | UNLOGGED }
2006-07-04 20:07:24 +02:00
SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
2006-07-02 03:58:36 +02:00
INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
2011-04-21 03:35:15 +02:00
OF <replaceable class="PARAMETER">type_name</replaceable>
NOT OF
Allow CURRENT/SESSION_USER to be used in certain commands
Commands such as ALTER USER, ALTER GROUP, ALTER ROLE, GRANT, and the
various ALTER OBJECT / OWNER TO, as well as ad-hoc clauses related to
roles such as the AUTHORIZATION clause of CREATE SCHEMA, the FOR clause
of CREATE USER MAPPING, and the FOR ROLE clause of ALTER DEFAULT
PRIVILEGES can now take the keywords CURRENT_USER and SESSION_USER as
user specifiers in place of an explicit user name.
This commit also fixes some quite ugly handling of special standards-
mandated syntax in CREATE USER MAPPING, which in particular would fail
to work in presence of a role named "current_user".
The special role specifiers PUBLIC and NONE also have more consistent
handling now.
Also take the opportunity to add location tracking to user specifiers.
Authors: Kyotaro Horiguchi. Heavily reworked by Álvaro Herrera.
Reviewed by: Rushabh Lathia, Adam Brightwell, Marti Raudsepp.
2015-03-09 19:41:54 +01:00
OWNER TO { <replaceable class="PARAMETER">new_owner</replaceable> | CURRENT_USER | SESSION_USER }
2015-05-22 14:42:15 +02:00
REPLICA IDENTITY { DEFAULT | USING INDEX <replaceable class="PARAMETER">index_name</replaceable> | FULL | NOTHING }
2011-01-25 21:42:03 +01:00
<phrase>and <replaceable class="PARAMETER">table_constraint_using_index</replaceable> is:</phrase>
[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
{ UNIQUE | PRIMARY KEY } USING INDEX <replaceable class="PARAMETER">index_name</replaceable>
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
2003-04-15 15:25:08 +02:00
</synopsis>
1999-07-06 19:16:42 +02:00
</refsynopsisdiv>
1998-05-13 07:34:00 +02:00
2003-04-15 15:25:08 +02:00
<refsect1>
<title>Description</title>
1999-07-06 19:16:42 +02:00
<para>
1998-09-16 16:43:12 +02:00
<command>ALTER TABLE</command> changes the definition of an existing table.
2014-04-06 17:13:43 +02:00
There are several subforms described below. Note that the lock level required
may differ for each subform. An <literal>ACCESS EXCLUSIVE</literal> lock is held
unless explicitly noted. When multiple subcommands are listed, the lock
held will be the strictest one required from any subcommand.
1999-07-06 19:16:42 +02:00
2002-04-23 04:07:16 +02:00
<variablelist>
<varlistentry>
2003-04-15 15:25:08 +02:00
<term><literal>ADD COLUMN</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
2005-08-01 18:11:14 +02:00
This form adds a new column to the table, using the same syntax as
2010-04-03 09:23:02 +02:00
<xref linkend="SQL-CREATETABLE">.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
2002-08-02 20:15:10 +02:00
<varlistentry>
2009-07-20 04:42:28 +02:00
<term><literal>DROP COLUMN [ IF EXISTS ]</literal></term>
2002-08-02 20:15:10 +02:00
<listitem>
<para>
2003-04-15 15:25:08 +02:00
This form drops a column from a table. Indexes and
2002-08-02 20:15:10 +02:00
table constraints involving the column will be automatically
dropped as well. You will need to say <literal>CASCADE</> if
2003-04-15 15:25:08 +02:00
anything outside the table depends on the column, for example,
foreign key references or views.
2009-08-03 00:14:53 +02:00
If <literal>IF EXISTS</literal> is specified and the column
does not exist, no error is thrown. In this case a notice
2009-07-20 04:42:28 +02:00
is issued instead.
2002-08-02 20:15:10 +02:00
</para>
</listitem>
</varlistentry>
2004-05-05 06:48:48 +02:00
<varlistentry>
2008-10-21 10:38:16 +02:00
<term><literal>SET DATA TYPE</literal></term>
2004-05-05 06:48:48 +02:00
<listitem>
<para>
This form changes the type of a column of a table. Indexes and
simple table constraints involving the column will be automatically
converted to use the new column type by reparsing the originally
Remove collation information from TypeName, where it does not belong.
The initial collations patch treated a COLLATE spec as part of a TypeName,
following what can only be described as brain fade on the part of the SQL
committee. It's a lot more reasonable to treat COLLATE as a syntactically
separate object, so that it can be added in only the productions where it
actually belongs, rather than needing to reject it in a boatload of places
where it doesn't belong (something the original patch mostly failed to do).
In addition this change lets us meet the spec's requirement to allow
COLLATE anywhere in the clauses of a ColumnDef, and it avoids unfriendly
behavior for constructs such as "foo::type COLLATE collation".
To do this, pull collation information out of TypeName and put it in
ColumnDef instead, thus reverting most of the collation-related changes in
parse_type.c's API. I made one additional structural change, which was to
use a ColumnDef as an intermediate node in AT_AlterColumnType AlterTableCmd
nodes. This provides enough room to get rid of the "transform" wart in
AlterTableCmd too, since the ColumnDef can carry the USING expression
easily enough.
Also fix some other minor bugs that have crept in in the same areas,
like failure to copy recently-added fields of ColumnDef in copyfuncs.c.
While at it, document the formerly secret ability to specify a collation
in ALTER TABLE ALTER COLUMN TYPE, ALTER TYPE ADD ATTRIBUTE, and
ALTER TYPE ALTER ATTRIBUTE TYPE; and correct some misstatements about
what the default collation selection will be when COLLATE is omitted.
BTW, the three-parameter form of format_type() should go away too,
since it just contributes to the confusion in this area; but I'll do
that in a separate patch.
2011-03-10 04:38:52 +01:00
supplied expression.
The optional <literal>COLLATE</literal> clause specifies a collation
for the new column; if omitted, the collation is the default for the
new column type.
The optional <literal>USING</literal>
2004-05-05 06:48:48 +02:00
clause specifies how to compute the new column value from the old;
if omitted, the default conversion is the same as an assignment
cast from old data type to new. A <literal>USING</literal>
clause must be provided if there is no implicit or assignment
cast from old to new type.
</para>
</listitem>
</varlistentry>
2002-04-23 04:07:16 +02:00
<varlistentry>
2003-04-15 15:25:08 +02:00
<term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
2003-04-15 15:25:08 +02:00
These forms set or remove the default value for a column.
2012-12-09 00:25:48 +01:00
Default values only apply in subsequent <command>INSERT</command>
or <command>UPDATE</> commands; they do not cause rows already in the
table to change.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2003-04-15 15:25:08 +02:00
<term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
2003-04-15 15:25:08 +02:00
These forms change whether a column is marked to allow null
values or to reject null values. You can only use <literal>SET
NOT NULL</> when the column contains no null values.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2003-04-15 15:25:08 +02:00
<term><literal>SET STATISTICS</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
This form
sets the per-column statistics-gathering target for subsequent
2010-04-03 09:23:02 +02:00
<xref linkend="sql-analyze"> operations.
2008-12-13 20:13:44 +01:00
The target can be set in the range 0 to 10000; alternatively, set it
2004-03-01 18:58:39 +01:00
to -1 to revert to using the system default statistics
2005-01-04 01:39:53 +01:00
target (<xref linkend="guc-default-statistics-target">).
For more information on the use of statistics by the
2004-03-01 18:58:39 +01:00
<productname>PostgreSQL</productname> query planner, refer to
<xref linkend="planner-stats">.
2002-04-23 04:07:16 +02:00
</para>
2014-04-06 17:13:43 +02:00
<para>
2015-01-14 22:40:58 +01:00
<literal>SET STATISTICS</literal> acquires a
<literal>SHARE UPDATE EXCLUSIVE</literal> lock.
2014-04-06 17:13:43 +02:00
</para>
2002-04-23 04:07:16 +02:00
</listitem>
</varlistentry>
2009-08-03 00:14:53 +02:00
<varlistentry>
2010-01-22 18:30:24 +01:00
<term><literal>SET ( <replaceable class="PARAMETER">attribute_option</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )</literal></term>
<term><literal>RESET ( <replaceable class="PARAMETER">attribute_option</replaceable> [, ... ] )</literal></term>
2009-08-03 00:14:53 +02:00
<listitem>
<para>
2010-09-16 20:15:21 +02:00
This form sets or resets per-attribute options. Currently, the only
defined per-attribute options are <literal>n_distinct</> and
2010-01-22 17:40:19 +01:00
<literal>n_distinct_inherited</>, which override the
2010-09-16 20:15:21 +02:00
number-of-distinct-values estimates made by subsequent
2010-04-03 09:23:02 +02:00
<xref linkend="sql-analyze">
2010-01-22 17:40:19 +01:00
operations. <literal>n_distinct</> affects the statistics for the table
itself, while <literal>n_distinct_inherited</> affects the statistics
2010-09-16 20:15:21 +02:00
gathered for the table plus its inheritance children. When set to a
2010-01-22 17:40:19 +01:00
positive value, <command>ANALYZE</> will assume that the column contains
exactly the specified number of distinct nonnull values. When set to a
negative value, which must be greater
2009-08-03 00:14:53 +02:00
than or equal to -1, <command>ANALYZE</> will assume that the number of
distinct nonnull values in the column is linear in the size of the
table; the exact count is to be computed by multiplying the estimated
table size by the absolute value of the given number. For example,
a value of -1 implies that all values in the column are distinct, while
a value of -0.5 implies that each value appears twice on the average.
This can be useful when the size of the table changes over time, since
the multiplication by the number of rows in the table is not performed
until query planning time. Specify a value of 0 to revert to estimating
the number of distinct values normally. For more information on the use
of statistics by the <productname>PostgreSQL</productname> query
planner, refer to <xref linkend="planner-stats">.
</para>
2014-04-06 17:13:43 +02:00
<para>
Changing per-attribute options acquires a
<literal>SHARE UPDATE EXCLUSIVE</literal> lock.
</para>
2009-08-03 00:14:53 +02:00
</listitem>
</varlistentry>
2002-04-23 04:07:16 +02:00
<varlistentry>
2014-05-07 03:28:58 +02:00
<term>
<literal>SET STORAGE</literal>
<indexterm>
<primary>TOAST</primary>
<secondary>per-column storage settings</secondary>
</indexterm>
</term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
This form sets the storage mode for a column. This controls whether this
2010-05-13 20:54:18 +02:00
column is held inline or in a secondary <acronym>TOAST</> table, and
whether the data
2002-04-23 04:07:16 +02:00
should be compressed or not. <literal>PLAIN</literal> must be used
2003-04-15 15:25:08 +02:00
for fixed-length values such as <type>integer</type> and is
2002-04-23 04:07:16 +02:00
inline, uncompressed. <literal>MAIN</literal> is for inline,
compressible data. <literal>EXTERNAL</literal> is for external,
2003-04-15 15:25:08 +02:00
uncompressed data, and <literal>EXTENDED</literal> is for external,
2005-01-10 01:04:43 +01:00
compressed data. <literal>EXTENDED</literal> is the default for most
data types that support non-<literal>PLAIN</literal> storage.
2010-05-13 20:54:18 +02:00
Use of <literal>EXTERNAL</literal> will make substring operations on
very large <type>text</type> and <type>bytea</type> values run faster,
at the penalty of increased storage space. Note that
2005-01-10 01:04:43 +01:00
<literal>SET STORAGE</> doesn't itself change anything in the table,
it just sets the strategy to be pursued during future table updates.
See <xref linkend="storage-toast"> for more information.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
2003-02-13 06:20:05 +01:00
<varlistentry>
2011-06-16 01:05:11 +02:00
<term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable> [ NOT VALID ]</literal></term>
2003-02-13 06:20:05 +01:00
<listitem>
<para>
2004-05-05 06:48:48 +02:00
This form adds a new constraint to a table using the same syntax as
2011-06-16 01:05:11 +02:00
<xref linkend="SQL-CREATETABLE">, plus the option <literal>NOT
VALID</literal>, which is currently only allowed for foreign key
2011-06-02 00:43:50 +02:00
and CHECK constraints.
2011-06-16 01:05:11 +02:00
If the constraint is marked <literal>NOT VALID</literal>, the
potentially-lengthy initial check to verify that all rows in the table
satisfy the constraint is skipped. The constraint will still be
enforced against subsequent inserts or updates (that is, they'll fail
2011-06-02 00:43:50 +02:00
unless there is a matching row in the referenced table, in the case
of foreign keys; and they'll fail unless the new row matches the
specified check constraints). But the
2011-06-16 01:05:11 +02:00
database will not assume that the constraint holds for all rows in
the table, until it is validated by using the <literal>VALIDATE
CONSTRAINT</literal> option.
2003-12-01 23:08:02 +01:00
</para>
2003-02-13 06:20:05 +01:00
</listitem>
</varlistentry>
2011-01-25 21:42:03 +01:00
<varlistentry>
<term><literal>ADD <replaceable class="PARAMETER">table_constraint_using_index</replaceable></literal></term>
<listitem>
<para>
This form adds a new <literal>PRIMARY KEY</> or <literal>UNIQUE</>
constraint to a table based on an existing unique index. All the
columns of the index will be included in the constraint.
</para>
<para>
The index cannot have expression columns nor be a partial index.
Also, it must be a b-tree index with default sort ordering. These
restrictions ensure that the index is equivalent to one that would be
built by a regular <literal>ADD PRIMARY KEY</> or <literal>ADD UNIQUE</>
command.
</para>
<para>
If <literal>PRIMARY KEY</> is specified, and the index's columns are not
already marked <literal>NOT NULL</>, then this command will attempt to
do <literal>ALTER COLUMN SET NOT NULL</> against each such column.
That requires a full table scan to verify the column(s) contain no
nulls. In all other cases, this is a fast operation.
</para>
<para>
If a constraint name is provided then the index will be renamed to match
the constraint name. Otherwise the constraint will be named the same as
the index.
</para>
<para>
After this command is executed, the index is <quote>owned</> by the
constraint, in the same way as if the index had been built by
a regular <literal>ADD PRIMARY KEY</> or <literal>ADD UNIQUE</>
command. In particular, dropping the constraint will make the index
disappear too.
</para>
<note>
<para>
Adding a constraint using an existing index can be helpful in
situations where a new constraint needs to be added without blocking
table updates for a long time. To do that, create the index using
<command>CREATE INDEX CONCURRENTLY</>, and then install it as an
official constraint using this syntax. See the example below.
</para>
</note>
</listitem>
</varlistentry>
2013-06-29 01:27:30 +02:00
<varlistentry>
<term><literal>ALTER CONSTRAINT</literal></term>
<listitem>
<para>
This form alters the attributes of a constraint that was previously
created. Currently only foreign key constraints may be altered.
</para>
</listitem>
</varlistentry>
2011-06-16 01:05:11 +02:00
<varlistentry>
<term><literal>VALIDATE CONSTRAINT</literal></term>
<listitem>
<para>
2011-06-02 00:43:50 +02:00
This form validates a foreign key or check constraint that was previously created
2011-06-16 01:05:11 +02:00
as <literal>NOT VALID</literal>, by scanning the table to ensure there
2011-06-02 00:43:50 +02:00
are no rows for which the constraint is not satisfied.
Nothing happens if the constraint is already marked valid.
2013-06-18 13:09:39 +02:00
</para>
<para>
2014-04-06 17:13:43 +02:00
Validation can be a long process on larger tables. The value of separating
2013-06-18 13:09:39 +02:00
validation from initial creation is that you can defer validation to less
busy times, or can be used to give additional time to correct pre-existing
2014-04-06 17:13:43 +02:00
errors while preventing new errors. Note also that validation on its own
does not prevent normal write commands against the table while it runs.
</para>
<para>
Validation acquires only a <literal>SHARE UPDATE EXCLUSIVE</literal> lock
on the table being altered. If the constraint is a foreign key then
a <literal>ROW SHARE</literal> lock is also required on
the table referenced by the constraint.
2011-06-16 01:05:11 +02:00
</para>
</listitem>
</varlistentry>
2002-04-23 04:07:16 +02:00
<varlistentry>
2009-07-20 04:42:28 +02:00
<term><literal>DROP CONSTRAINT [ IF EXISTS ]</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
2005-12-08 22:35:36 +01:00
This form drops the specified constraint on a table.
2009-07-20 04:42:28 +02:00
If <literal>IF EXISTS</literal> is specified and the constraint
does not exist, no error is thrown. In this case a notice is issued instead.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
2005-08-24 19:24:19 +02:00
<varlistentry>
2007-03-20 00:38:32 +01:00
<term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] TRIGGER</literal></term>
2005-08-24 19:24:19 +02:00
<listitem>
<para>
2007-03-20 00:38:32 +01:00
These forms configure the firing of trigger(s) belonging to the table.
2005-08-24 19:24:19 +02:00
A disabled trigger is still known to the system, but is not executed
when its triggering event occurs. For a deferred trigger, the enable
status is checked when the event occurs, not when the trigger function
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
is actually executed. One can disable or enable a single
2005-08-24 19:24:19 +02:00
trigger specified by name, or all triggers on the table, or only
2010-06-09 19:48:10 +02:00
user triggers (this option excludes internally generated constraint
triggers such as those that are used to implement foreign key
constraints or deferrable uniqueness and exclusion constraints).
Disabling or enabling internally generated constraint triggers
2005-08-24 19:24:19 +02:00
requires superuser privileges; it should be done with caution since
of course the integrity of the constraint cannot be guaranteed if the
triggers are not executed.
2007-03-22 16:45:56 +01:00
The trigger firing mechanism is also affected by the configuration
2007-11-28 16:42:31 +01:00
variable <xref linkend="guc-session-replication-role">. Simply enabled
2007-03-22 16:45:56 +01:00
triggers will fire when the replication role is <quote>origin</>
2009-02-11 22:11:16 +01:00
(the default) or <quote>local</>. Triggers configured as <literal>ENABLE
REPLICA</literal> will only fire if the session is in <quote>replica</>
mode, and triggers configured as <literal>ENABLE ALWAYS</literal> will
fire regardless of the current replication mode.
2007-03-20 00:38:32 +01:00
</para>
2015-04-05 17:37:08 +02:00
<para>
This command acquires a <literal>SHARE ROW EXCLUSIVE</literal> lock.
</para>
2007-03-20 00:38:32 +01:00
</listitem>
</varlistentry>
<varlistentry>
<term><literal>DISABLE</literal>/<literal>ENABLE [ REPLICA | ALWAYS ] RULE</literal></term>
<listitem>
<para>
These forms configure the firing of rewrite rules belonging to the table.
A disabled rule is still known to the system, but is not applied
2007-03-22 16:45:56 +01:00
during query rewriting. The semantics are as for disabled/enabled
2007-05-18 01:36:04 +02:00
triggers. This configuration is ignored for <literal>ON SELECT</literal> rules, which
2007-03-22 16:45:56 +01:00
are always applied in order to keep views working even if the current
session is in a non-default replication role.
2005-08-24 19:24:19 +02:00
</para>
</listitem>
</varlistentry>
Row-Level Security Policies (RLS)
Building on the updatable security-barrier views work, add the
ability to define policies on tables to limit the set of rows
which are returned from a query and which are allowed to be added
to a table. Expressions defined by the policy for filtering are
added to the security barrier quals of the query, while expressions
defined to check records being added to a table are added to the
with-check options of the query.
New top-level commands are CREATE/ALTER/DROP POLICY and are
controlled by the table owner. Row Security is able to be enabled
and disabled by the owner on a per-table basis using
ALTER TABLE .. ENABLE/DISABLE ROW SECURITY.
Per discussion, ROW SECURITY is disabled on tables by default and
must be enabled for policies on the table to be used. If no
policies exist on a table with ROW SECURITY enabled, a default-deny
policy is used and no records will be visible.
By default, row security is applied at all times except for the
table owner and the superuser. A new GUC, row_security, is added
which can be set to ON, OFF, or FORCE. When set to FORCE, row
security will be applied even for the table owner and superusers.
When set to OFF, row security will be disabled when allowed and an
error will be thrown if the user does not have rights to bypass row
security.
Per discussion, pg_dump sets row_security = OFF by default to ensure
that exports and backups will have all data in the table or will
error if there are insufficient privileges to bypass row security.
A new option has been added to pg_dump, --enable-row-security, to
ask pg_dump to export with row security enabled.
A new role capability, BYPASSRLS, which can only be set by the
superuser, is added to allow other users to be able to bypass row
security using row_security = OFF.
Many thanks to the various individuals who have helped with the
design, particularly Robert Haas for his feedback.
Authors include Craig Ringer, KaiGai Kohei, Adam Brightwell, Dean
Rasheed, with additional changes and rework by me.
Reviewers have included all of the above, Greg Smith,
Jeff McCormick, and Robert Haas.
2014-09-19 17:18:35 +02:00
<varlistentry>
<term><literal>DISABLE</literal>/<literal>ENABLE ROW LEVEL SECURITY</literal></term>
<listitem>
<para>
These forms control the application of row security policies belonging
to the table. If enabled and no policies exist for the table, then a
default-deny policy is applied. Note that policies can exist for a table
Code review for row security.
Buildfarm member tick identified an issue where the policies in the
relcache for a relation were were being replaced underneath a running
query, leading to segfaults while processing the policies to be added
to a query. Similar to how TupleDesc RuleLocks are handled, add in a
equalRSDesc() function to check if the policies have actually changed
and, if not, swap back the rsdesc field (using the original instead of
the temporairly built one; the whole structure is swapped and then
specific fields swapped back). This now passes a CLOBBER_CACHE_ALWAYS
for me and should resolve the buildfarm error.
In addition to addressing this, add a new chapter in Data Definition
under Privileges which explains row security and provides examples of
its usage, change \d to always list policies (even if row security is
disabled- but note that it is disabled, or enabled with no policies),
rework check_role_for_policy (it really didn't need the entire policy,
but it did need to be using has_privs_of_role()), and change the field
in pg_class to relrowsecurity from relhasrowsecurity, based on
Heikki's suggestion. Also from Heikki, only issue SET ROW_SECURITY in
pg_restore when talking to a 9.5+ server, list Bypass RLS in \du, and
document --enable-row-security options for pg_dump and pg_restore.
Lastly, fix a number of minor whitespace and typo issues from Heikki,
Dimitri, add a missing #include, per Peter E, fix a few minor
variable-assigned-but-not-used and resource leak issues from Coverity
and add tab completion for role attribute bypassrls as well.
2014-09-24 22:32:22 +02:00
even if row level security is disabled - in this case, the policies will
Row-Level Security Policies (RLS)
Building on the updatable security-barrier views work, add the
ability to define policies on tables to limit the set of rows
which are returned from a query and which are allowed to be added
to a table. Expressions defined by the policy for filtering are
added to the security barrier quals of the query, while expressions
defined to check records being added to a table are added to the
with-check options of the query.
New top-level commands are CREATE/ALTER/DROP POLICY and are
controlled by the table owner. Row Security is able to be enabled
and disabled by the owner on a per-table basis using
ALTER TABLE .. ENABLE/DISABLE ROW SECURITY.
Per discussion, ROW SECURITY is disabled on tables by default and
must be enabled for policies on the table to be used. If no
policies exist on a table with ROW SECURITY enabled, a default-deny
policy is used and no records will be visible.
By default, row security is applied at all times except for the
table owner and the superuser. A new GUC, row_security, is added
which can be set to ON, OFF, or FORCE. When set to FORCE, row
security will be applied even for the table owner and superusers.
When set to OFF, row security will be disabled when allowed and an
error will be thrown if the user does not have rights to bypass row
security.
Per discussion, pg_dump sets row_security = OFF by default to ensure
that exports and backups will have all data in the table or will
error if there are insufficient privileges to bypass row security.
A new option has been added to pg_dump, --enable-row-security, to
ask pg_dump to export with row security enabled.
A new role capability, BYPASSRLS, which can only be set by the
superuser, is added to allow other users to be able to bypass row
security using row_security = OFF.
Many thanks to the various individuals who have helped with the
design, particularly Robert Haas for his feedback.
Authors include Craig Ringer, KaiGai Kohei, Adam Brightwell, Dean
Rasheed, with additional changes and rework by me.
Reviewers have included all of the above, Greg Smith,
Jeff McCormick, and Robert Haas.
2014-09-19 17:18:35 +02:00
NOT be applied and the policies will be ignored.
See also
<xref linkend="SQL-CREATEPOLICY">.
</para>
</listitem>
</varlistentry>
2004-07-12 01:13:58 +02:00
<varlistentry>
2012-05-21 18:06:56 +02:00
<term><literal>CLUSTER ON</literal></term>
2004-07-12 01:13:58 +02:00
<listitem>
<para>
2009-02-11 22:11:16 +01:00
This form selects the default index for future
2010-04-03 09:23:02 +02:00
<xref linkend="SQL-CLUSTER">
2004-07-12 01:13:58 +02:00
operations. It does not actually re-cluster the table.
</para>
2014-04-06 17:13:43 +02:00
<para>
Changing cluster options acquires a <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
</para>
2004-07-12 01:13:58 +02:00
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SET WITHOUT CLUSTER</literal></term>
<listitem>
<para>
This form removes the most recently used
2010-04-03 09:23:02 +02:00
<xref linkend="SQL-CLUSTER">
2004-07-12 01:13:58 +02:00
index specification from the table. This affects
future cluster operations that don't specify an index.
</para>
2014-04-06 17:13:43 +02:00
<para>
Changing cluster options acquires a <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
</para>
2004-07-12 01:13:58 +02:00
</listitem>
</varlistentry>
2009-02-11 22:11:16 +01:00
<varlistentry>
<term><literal>SET WITH OIDS</literal></term>
<listitem>
<para>
This form adds an <literal>oid</literal> system column to the
table (see <xref linkend="ddl-system-columns">).
It does nothing if the table already has OIDs.
</para>
<para>
Note that this is not equivalent to <literal>ADD COLUMN oid oid</>;
that would add a normal column that happened to be named
<literal>oid</>, not a system column.
</para>
</listitem>
</varlistentry>
2002-04-23 04:07:16 +02:00
<varlistentry>
2004-05-05 06:48:48 +02:00
<term><literal>SET WITHOUT OIDS</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
2004-05-05 06:48:48 +02:00
This form removes the <literal>oid</literal> system column from the
table. This is exactly equivalent to
<literal>DROP COLUMN oid RESTRICT</literal>,
except that it will not complain if there is already no
<literal>oid</literal> column.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
2014-08-22 20:27:00 +02:00
<varlistentry>
<term><literal>SET TABLESPACE</literal></term>
<listitem>
<para>
This form changes the table's tablespace to the specified tablespace and
moves the data file(s) associated with the table to the new tablespace.
Indexes on the table, if any, are not moved; but they can be moved
separately with additional <literal>SET TABLESPACE</literal> commands.
All tables in the current database in a tablespace can be moved by using
the <literal>ALL IN TABLESPACE</literal> form, which will lock all tables
to be moved first and then move each one. This form also supports
<literal>OWNED BY</literal>, which will only move tables owned by the
roles specified. If the <literal>NOWAIT</literal> option is specified
then the command will fail if it is unable to acquire all of the locks
required immediately. Note that system catalogs are not moved by this
command, use <command>ALTER DATABASE</command> or explicit
<command>ALTER TABLE</command> invocations instead if desired. The
<literal>information_schema</literal> relations are not considered part
of the system catalogs and will be moved.
See also
<xref linkend="SQL-CREATETABLESPACE">.
</para>
</listitem>
</varlistentry>
<varlistentry>
2015-05-22 14:42:15 +02:00
<term><literal>SET { LOGGED | UNLOGGED }</literal></term>
2014-08-22 20:27:00 +02:00
<listitem>
<para>
This form changes the table from unlogged to logged or vice-versa
(see <xref linkend="SQL-CREATETABLE-UNLOGGED">). It cannot be applied
to a temporary table.
</para>
</listitem>
</varlistentry>
2006-07-02 03:58:36 +02:00
<varlistentry>
2006-07-04 20:07:24 +02:00
<term><literal>SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )</literal></term>
2006-07-02 03:58:36 +02:00
<listitem>
<para>
2006-07-04 20:07:24 +02:00
This form changes one or more storage parameters for the table. See
2009-02-09 21:57:59 +01:00
<xref linkend="SQL-CREATETABLE-storage-parameters"
2010-04-03 09:23:02 +02:00
endterm="SQL-CREATETABLE-storage-parameters-title">
2006-07-04 20:07:24 +02:00
for details on the available parameters. Note that the table contents
will not be modified immediately by this command; depending on the
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
parameter you might need to rewrite the table to get the desired effects.
2011-02-04 19:08:56 +01:00
That can be done with <link linkend="SQL-VACUUM">VACUUM
FULL</>, <xref linkend="SQL-CLUSTER"> or one of the forms
of <command>ALTER TABLE</> that forces a table rewrite.
2006-07-04 20:07:24 +02:00
</para>
<note>
<para>
While <command>CREATE TABLE</> allows <literal>OIDS</> to be specified
in the <literal>WITH (<replaceable
class="PARAMETER">storage_parameter</>)</literal> syntax,
<command>ALTER TABLE</> does not treat <literal>OIDS</> as a
2009-02-11 22:11:16 +01:00
storage parameter. Instead use the <literal>SET WITH OIDS</>
and <literal>SET WITHOUT OIDS</> forms to change OID status.
2006-07-04 20:07:24 +02:00
</para>
</note>
</listitem>
</varlistentry>
2006-07-02 03:58:36 +02:00
2006-07-04 20:07:24 +02:00
<varlistentry>
<term><literal>RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )</literal></term>
<listitem>
<para>
This form resets one or more storage parameters to their
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
defaults. As with <literal>SET</>, a table rewrite might be
2006-07-04 20:07:24 +02:00
needed to update the table entirely.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
<listitem>
<para>
2006-10-13 23:43:19 +02:00
This form adds the target table as a new child of the specified parent
table. Subsequently, queries against the parent will include records
of the target table. To be added as a child, the target table must
already contain all the same columns as the parent (it could have
additional columns, too). The columns must have matching data types,
2006-07-02 03:58:36 +02:00
and if they have <literal>NOT NULL</literal> constraints in the parent
then they must also have <literal>NOT NULL</literal> constraints in the
child.
2006-07-04 20:07:24 +02:00
</para>
2006-07-02 03:58:36 +02:00
2006-07-04 20:07:24 +02:00
<para>
2006-10-13 23:43:19 +02:00
There must also be matching child-table constraints for all
2012-01-16 23:19:42 +01:00
<literal>CHECK</literal> constraints of the parent, except those
2012-07-24 21:49:54 +02:00
marked non-inheritable (that is, created with <literal>ALTER TABLE ... ADD CONSTRAINT ... NO INHERIT</literal>)
2012-01-16 23:19:42 +01:00
in the parent, which are ignored; all child-table constraints matched
must not be marked non-inheritable.
Currently
2006-07-02 03:58:36 +02:00
<literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and
2006-10-13 23:43:19 +02:00
<literal>FOREIGN KEY</literal> constraints are not considered, but
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
this might change in the future.
2006-07-02 03:58:36 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
<listitem>
<para>
2006-10-13 23:43:19 +02:00
This form removes the target table from the list of children of the
specified parent table.
2006-07-04 20:07:24 +02:00
Queries against the parent table will no longer include records drawn
from the target table.
2006-07-02 03:58:36 +02:00
</para>
</listitem>
</varlistentry>
2011-04-21 03:35:15 +02:00
<varlistentry>
<term><literal>OF <replaceable class="PARAMETER">type_name</replaceable></literal></term>
<listitem>
<para>
This form links the table to a composite type as though <command>CREATE
TABLE OF</> had formed it. The table's list of column names and types
must precisely match that of the composite type; the presence of
an <literal>oid</> system column is permitted to differ. The table must
not inherit from any other table. These restrictions ensure
that <command>CREATE TABLE OF</> would permit an equivalent table
definition.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>NOT OF</literal></term>
<listitem>
<para>
This form dissociates a typed table from its type.
</para>
</listitem>
</varlistentry>
2002-04-23 04:07:16 +02:00
<varlistentry>
2003-04-15 15:25:08 +02:00
<term><literal>OWNER</literal></term>
2002-04-23 04:07:16 +02:00
<listitem>
<para>
2014-04-14 16:43:53 +02:00
This form changes the owner of the table, sequence, view, materialized view,
or foreign table to the specified user.
2002-04-23 04:07:16 +02:00
</para>
</listitem>
</varlistentry>
2003-03-20 19:52:48 +01:00
2014-03-18 18:20:01 +01:00
<varlistentry id="SQL-CREATETABLE-REPLICA-IDENTITY">
2013-11-08 18:30:43 +01:00
<term><literal>REPLICA IDENTITY</literal></term>
<listitem>
<para>
This form changes the information which is written to the write-ahead log
to identify rows which are updated or deleted. This option has no effect
2014-04-15 19:28:54 +02:00
except when logical replication is in use. <literal>DEFAULT</>
(the default for non-system tables) records the
2013-11-08 18:30:43 +01:00
old values of the columns of the primary key, if any. <literal>USING INDEX</>
records the old values of the columns covered by the named index, which
must be unique, not partial, not deferrable, and include only columns marked
<literal>NOT NULL</>. <literal>FULL</> records the old values of all columns
in the row. <literal>NOTHING</> records no information about the old row.
2014-04-15 19:28:54 +02:00
(This is the default for system tables.)
2013-11-08 18:30:43 +01:00
In all cases, no old values are logged unless at least one of the columns
that would be logged differs between the old and new versions of the row.
</para>
</listitem>
</varlistentry>
2004-05-05 06:48:48 +02:00
<varlistentry>
<term><literal>RENAME</literal></term>
<listitem>
<para>
The <literal>RENAME</literal> forms change the name of a table
2014-04-14 16:43:53 +02:00
(or an index, sequence, view, materialized view, or foreign table), the name
of an individual column in a table, or the name of a constraint of the table.
There is no effect on the stored data.
2004-05-05 06:48:48 +02:00
</para>
</listitem>
</varlistentry>
2005-08-01 18:11:14 +02:00
<varlistentry>
<term><literal>SET SCHEMA</literal></term>
<listitem>
<para>
This form moves the table into another schema. Associated indexes,
2006-08-21 02:57:26 +02:00
constraints, and sequences owned by table columns are moved as well.
2005-08-01 18:11:14 +02:00
</para>
</listitem>
</varlistentry>
2002-04-23 04:07:16 +02:00
</variablelist>
2003-04-15 15:25:08 +02:00
</para>
2002-04-23 04:07:16 +02:00
2004-05-05 06:48:48 +02:00
<para>
2014-08-22 01:06:17 +02:00
All the actions except <literal>RENAME</literal>,
<literal>SET TABLESPACE</literal> and <literal>SET SCHEMA</literal>
2005-08-01 18:11:14 +02:00
can be combined into
2004-05-05 06:48:48 +02:00
a list of multiple alterations to apply in parallel. For example, it
is possible to add several columns and/or alter the type of several
columns in a single command. This is particularly useful with large
tables, since only one pass over the table need be made.
</para>
1999-07-06 19:16:42 +02:00
<para>
2005-08-22 23:32:01 +02:00
You must own the table to use <command>ALTER TABLE</>.
2014-08-22 01:06:17 +02:00
To change the schema or tablespace of a table, you must also have
<literal>CREATE</literal> privilege on the new schema or tablespace.
2006-10-13 23:43:19 +02:00
To add the table as a new child of a parent table, you must own the
parent table as well.
2005-08-24 19:24:19 +02:00
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 table's schema. (These restrictions enforce that altering the owner
doesn't do anything you couldn't do by dropping and recreating the table.
However, a superuser can alter ownership of any table anyway.)
2011-12-19 23:05:19 +01:00
To add a column or alter a column type or use the <literal>OF</literal>
clause, you must also have <literal>USAGE</literal> privilege on the data
type.
1999-07-06 19:16:42 +02:00
</para>
2003-04-15 15:25:08 +02:00
</refsect1>
<refsect1>
<title>Parameters</title>
<variablelist>
2015-04-24 19:22:18 +02:00
<varlistentry>
<term><literal>IF EXISTS</literal></term>
<listitem>
<para>
Do not throw an error if the table does not exist. A notice is issued
in this case.
</para>
</listitem>
</varlistentry>
2003-04-15 15:25:08 +02:00
<varlistentry>
2003-09-22 02:16:58 +02:00
<term><replaceable class="PARAMETER">name</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2012-09-17 20:59:31 +02:00
The name (optionally schema-qualified) of an existing table to
alter. If <literal>ONLY</> is specified before the table name, only
that table is altered. If <literal>ONLY</> is not specified, the table
and all its descendant tables (if any) are altered. Optionally,
<literal>*</> can be specified after the table name to explicitly
indicate that descendant tables are included.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2012-06-22 00:06:14 +02:00
<term><replaceable class="PARAMETER">column_name</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2005-01-04 01:39:53 +01:00
Name of a new or existing column.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2012-06-22 00:06:14 +02:00
<term><replaceable class="PARAMETER">new_column_name</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2005-01-04 01:39:53 +01:00
New name for an existing column.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2004-07-12 01:13:58 +02:00
<term><replaceable class="PARAMETER">new_name</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2005-01-04 01:39:53 +01:00
New name for the table.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2014-04-28 16:10:51 +02:00
<term><replaceable class="PARAMETER">data_type</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2005-01-04 01:39:53 +01:00
Data type of the new column, or new data type for an existing
column.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">table_constraint</replaceable></term>
<listitem>
<para>
2005-01-04 01:39:53 +01:00
New table constraint for the table.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">constraint_name</replaceable></term>
<listitem>
<para>
2014-04-14 16:52:07 +02:00
Name of a new or existing constraint.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2004-07-12 01:13:58 +02:00
<term><literal>CASCADE</literal></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2004-07-12 01:13:58 +02:00
Automatically drop objects that depend on the dropped column
2005-01-04 01:39:53 +01:00
or constraint (for example, views referencing the column).
2004-07-12 01:13:58 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>RESTRICT</literal></term>
<listitem>
<para>
Refuse to drop the column or constraint if there are any dependent
2005-01-04 01:39:53 +01:00
objects. This is the default behavior.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
2005-08-24 19:24:19 +02:00
<varlistentry>
<term><replaceable class="PARAMETER">trigger_name</replaceable></term>
<listitem>
<para>
Name of a single trigger to disable or enable.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>ALL</literal></term>
<listitem>
<para>
Disable or enable all triggers belonging to the table.
2010-06-09 19:48:10 +02:00
(This requires superuser privilege if any of the triggers are
internally generated constraint triggers such as those that are used
to implement foreign key constraints or deferrable uniqueness and
exclusion constraints.)
2005-08-24 19:24:19 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>USER</literal></term>
<listitem>
<para>
Disable or enable all triggers belonging to the table except for
2010-06-09 19:48:10 +02:00
internally generated constraint triggers such as those that are used
to implement foreign key constraints or deferrable uniqueness and
exclusion constraints.
2005-08-24 19:24:19 +02:00
</para>
</listitem>
</varlistentry>
2003-04-15 15:25:08 +02:00
<varlistentry>
<term><replaceable class="PARAMETER">index_name</replaceable></term>
<listitem>
<para>
2014-04-28 16:10:51 +02:00
The name of an existing index.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
2006-07-04 20:07:24 +02:00
<varlistentry>
<term><replaceable class="PARAMETER">storage_parameter</replaceable></term>
<listitem>
<para>
The name of a table storage parameter.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">value</replaceable></term>
<listitem>
<para>
The new value for a table storage parameter.
This might be a number or a word depending on the parameter.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">parent_table</replaceable></term>
<listitem>
<para>
A parent table to associate or de-associate with this table.
</para>
</listitem>
</varlistentry>
2003-04-15 15:25:08 +02:00
<varlistentry>
2004-07-12 01:13:58 +02:00
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2005-01-04 01:39:53 +01:00
The user name of the new owner of the table.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2005-08-01 18:11:14 +02:00
<term><replaceable class="PARAMETER">new_tablespace</replaceable></term>
<listitem>
<para>
The name of the tablespace to which the table will be moved.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_schema</replaceable></term>
2003-04-15 15:25:08 +02:00
<listitem>
<para>
2005-08-01 18:11:14 +02:00
The name of the schema to which the table will be moved.
2003-04-15 15:25:08 +02:00
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
1999-07-06 19:16:42 +02:00
<para>
2003-04-15 15:25:08 +02:00
The key word <literal>COLUMN</literal> is noise and can be omitted.
1999-07-06 19:16:42 +02:00
</para>
1998-05-13 07:34:00 +02:00
1999-07-22 17:09:15 +02:00
<para>
2004-05-05 06:48:48 +02:00
When a column is added with <literal>ADD COLUMN</literal>, all existing
rows in the table are initialized with the column's default value
(NULL if no <literal>DEFAULT</> clause is specified).
2014-04-03 18:38:00 +02:00
If there is no <literal>DEFAULT</> clause, this is merely a metadata
change and does not require any immediate update of the table's data;
the added NULL values are supplied on readout, instead.
2004-05-05 06:48:48 +02:00
</para>
<para>
2014-04-03 18:38:00 +02:00
Adding a column with a <literal>DEFAULT</> clause or changing the type of
an existing column will require the entire table and its indexes to be
rewritten. As an exception when changing the type of an existing column,
if the <literal>USING</> clause does not change the column
2011-02-15 05:17:35 +01:00
contents and the old type is either binary coercible to the new type or
2014-04-03 18:38:00 +02:00
an unconstrained domain over the new type, a table rewrite is not needed;
2011-02-15 05:17:35 +01:00
but any indexes on the affected columns must still be rebuilt. Adding or
removing a system <literal>oid</> column also requires rewriting the entire
table. Table and/or index rebuilds may take a significant amount of time
for a large table; and will temporarily require as much as double the disk
space.
2004-05-05 06:48:48 +02:00
</para>
<para>
Adding a <literal>CHECK</> or <literal>NOT NULL</> constraint requires
scanning the table to verify that existing rows meet the constraint.
</para>
<para>
The main reason for providing the option to specify multiple changes
in a single <command>ALTER TABLE</> is that multiple table scans or
rewrites can thereby be combined into a single pass over the table.
1999-07-22 17:09:15 +02:00
</para>
2002-08-02 20:15:10 +02:00
<para>
2003-04-15 15:25:08 +02:00
The <literal>DROP COLUMN</literal> form does not physically remove
2002-08-02 20:15:10 +02:00
the column, but simply makes it invisible to SQL operations. Subsequent
2004-05-27 05:30:11 +02:00
insert and update operations in the table will store a null value for the
column. Thus, dropping a column is quick but it will not immediately
2009-02-11 22:11:16 +01:00
reduce the on-disk size of your table, as the space occupied
2002-08-02 20:15:10 +02:00
by the dropped column is not reclaimed. The space will be
2009-02-11 22:11:16 +01:00
reclaimed over time as existing rows are updated. (These statements do
not apply when dropping the system <literal>oid</> column; that is done
with an immediate rewrite.)
2004-05-27 05:30:11 +02:00
</para>
<para>
2011-02-04 19:08:56 +01:00
To force an immediate rewrite of the table, you can use
<link linkend="SQL-VACUUM">VACUUM FULL</>, <xref linkend="SQL-CLUSTER">
2011-02-12 14:27:55 +01:00
or one of the forms of ALTER TABLE that forces a rewrite. This results in
no semantically-visible change in the table, but gets rid of
no-longer-useful data.
2002-08-02 20:15:10 +02:00
</para>
2004-10-22 19:20:05 +02:00
<para>
2011-01-26 00:50:35 +01:00
The <literal>USING</literal> option of <literal>SET DATA TYPE</> can actually
2004-10-22 19:20:05 +02:00
specify any expression involving the old values of the row; that is, it
can refer to other columns as well as the one being converted. This allows
2011-01-26 00:50:35 +01:00
very general conversions to be done with the <literal>SET DATA TYPE</>
2004-10-22 19:20:05 +02:00
syntax. Because of this flexibility, the <literal>USING</literal>
expression is not applied to the column's default value (if any); the
result might not be a constant expression as required for a default.
This means that when there is no implicit or assignment cast from old to
2011-01-26 00:50:35 +01:00
new type, <literal>SET DATA TYPE</> might fail to convert the default even
2004-10-22 19:20:05 +02:00
though a <literal>USING</literal> clause is supplied. In such cases,
drop the default with <literal>DROP DEFAULT</>, perform the <literal>ALTER
TYPE</>, and then use <literal>SET DEFAULT</> to add a suitable new
2005-01-04 01:39:53 +01:00
default. Similar considerations apply to indexes and constraints involving
the column.
2004-10-22 19:20:05 +02:00
</para>
2002-10-20 00:51:45 +02:00
<para>
2004-05-05 06:48:48 +02:00
If a table has any descendant tables, it is not permitted to add,
2012-03-10 19:19:13 +01:00
rename, or change the type of a column, or rename an inherited constraint
in the parent table without doing
2004-05-05 06:48:48 +02:00
the same to the descendants. That is, <command>ALTER TABLE ONLY</command>
2003-02-19 05:06:28 +01:00
will be rejected. This ensures that the descendants always have
columns matching the parent.
2002-10-20 00:51:45 +02:00
</para>
<para>
2003-02-19 05:06:28 +01:00
A recursive <literal>DROP COLUMN</literal> operation will remove a
descendant table's column only if the descendant does not inherit
that column from any other parents and never had an independent
definition of the column. A nonrecursive <literal>DROP
COLUMN</literal> (i.e., <command>ALTER TABLE ONLY ... DROP
COLUMN</command>) never removes any descendant columns, but
instead marks them as independently defined rather than inherited.
2002-10-20 00:51:45 +02:00
</para>
2005-08-24 19:24:19 +02:00
<para>
The <literal>TRIGGER</>, <literal>CLUSTER</>, <literal>OWNER</>,
and <literal>TABLESPACE</> actions never recurse to descendant tables;
that is, they always act as though <literal>ONLY</> were specified.
2012-04-24 03:59:17 +02:00
Adding a constraint recurses only for <literal>CHECK</> constraints
that are not marked <literal>NO INHERIT</>.
2005-08-24 19:24:19 +02:00
</para>
1999-07-22 17:09:15 +02:00
<para>
2003-04-15 15:25:08 +02:00
Changing any part of a system catalog table is not permitted.
1999-07-06 19:16:42 +02:00
</para>
1998-05-13 07:34:00 +02:00
1999-07-06 19:16:42 +02:00
<para>
2010-04-03 09:23:02 +02:00
Refer to <xref linkend="sql-createtable"> for a further description of valid
2004-03-24 10:49:20 +01:00
parameters. <xref linkend="ddl"> has further information on
2003-01-19 01:13:31 +01:00
inheritance.
1999-07-06 19:16:42 +02:00
</para>
</refsect1>
1998-05-13 07:34:00 +02:00
2003-04-15 15:25:08 +02:00
<refsect1>
<title>Examples</title>
1999-07-06 19:16:42 +02:00
<para>
2002-01-20 23:19:57 +01:00
To add a column of type <type>varchar</type> to a table:
2003-04-15 15:25:08 +02:00
<programlisting>
ALTER TABLE distributors ADD COLUMN address varchar(30);
</programlisting>
1999-07-06 19:16:42 +02:00
</para>
1998-05-13 07:34:00 +02:00
2002-08-02 20:15:10 +02:00
<para>
To drop a column from a table:
2003-04-15 15:25:08 +02:00
<programlisting>
2002-08-02 20:15:10 +02:00
ALTER TABLE distributors DROP COLUMN address RESTRICT;
2003-04-15 15:25:08 +02:00
</programlisting>
2002-08-02 20:15:10 +02:00
</para>
2004-05-05 06:48:48 +02:00
<para>
To change the types of two existing columns in one operation:
<programlisting>
ALTER TABLE distributors
ALTER COLUMN address TYPE varchar(80),
ALTER COLUMN name TYPE varchar(100);
</programlisting>
</para>
2005-01-14 02:16:52 +01:00
<para>
To change an integer column containing UNIX timestamps to <type>timestamp
with time zone</type> via a <literal>USING</literal> clause:
<programlisting>
ALTER TABLE foo
2008-10-21 10:38:16 +02:00
ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
2005-01-14 02:16:52 +01:00
USING
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
</programlisting>
</para>
2006-08-03 22:57:06 +02:00
<para>
The same, when the column has a default expression that won't automatically
2006-10-23 20:10:32 +02:00
cast to the new data type:
2006-08-03 22:57:06 +02:00
<programlisting>
ALTER TABLE foo
ALTER COLUMN foo_timestamp DROP DEFAULT,
ALTER COLUMN foo_timestamp TYPE timestamp with time zone
USING
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
ALTER COLUMN foo_timestamp SET DEFAULT now();
</programlisting>
</para>
1999-07-06 19:16:42 +02:00
<para>
1998-05-13 07:34:00 +02:00
To rename an existing column:
2003-04-15 15:25:08 +02:00
<programlisting>
1999-07-06 19:16:42 +02:00
ALTER TABLE distributors RENAME COLUMN address TO city;
2003-04-15 15:25:08 +02:00
</programlisting>
1999-07-06 19:16:42 +02:00
</para>
1998-05-13 07:34:00 +02:00
1999-07-06 19:16:42 +02:00
<para>
1998-05-13 07:34:00 +02:00
To rename an existing table:
2003-04-15 15:25:08 +02:00
<programlisting>
1999-07-06 19:16:42 +02:00
ALTER TABLE distributors RENAME TO suppliers;
2003-04-15 15:25:08 +02:00
</programlisting>
1999-07-06 19:16:42 +02:00
</para>
2002-04-01 06:35:40 +02:00
2012-03-10 19:19:13 +01:00
<para>
To rename an existing constraint:
<programlisting>
ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;
</programlisting>
</para>
2002-04-01 06:35:40 +02:00
<para>
2003-04-15 15:25:08 +02:00
To add a not-null constraint to a column:
<programlisting>
2002-04-01 06:35:40 +02:00
ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
2003-04-15 15:25:08 +02:00
</programlisting>
To remove a not-null constraint from a column:
<programlisting>
2002-04-01 06:35:40 +02:00
ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
2003-04-15 15:25:08 +02:00
</programlisting>
2002-04-01 06:35:40 +02:00
</para>
2000-04-11 16:43:54 +02:00
2009-02-11 22:11:16 +01:00
<para>
2008-05-10 01:32:05 +02:00
To add a check constraint to a table and all its children:
2003-04-15 15:25:08 +02:00
<programlisting>
2001-09-12 04:13:25 +02:00
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
2003-04-15 15:25:08 +02:00
</programlisting>
2001-05-09 15:27:15 +02:00
</para>
2011-12-05 19:10:18 +01:00
<para>
To add a check constraint only to a table and not to its children:
<programlisting>
2012-07-24 21:49:54 +02:00
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
2011-12-05 19:10:18 +01:00
</programlisting>
(The check constraint will not be inherited by future children, either.)
</para>
2009-02-11 22:11:16 +01:00
<para>
2001-05-30 15:01:08 +02:00
To remove a check constraint from a table and all its children:
2003-04-15 15:25:08 +02:00
<programlisting>
2002-07-12 20:43:19 +02:00
ALTER TABLE distributors DROP CONSTRAINT zipchk;
2003-04-15 15:25:08 +02:00
</programlisting>
2001-05-30 15:01:08 +02:00
</para>
2009-02-11 22:11:16 +01:00
<para>
2012-09-17 20:59:31 +02:00
To remove a check constraint from one table only:
2008-05-10 01:32:05 +02:00
<programlisting>
ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
</programlisting>
(The check constraint remains in place for any child tables.)
</para>
2009-02-11 22:11:16 +01:00
<para>
2000-04-11 16:43:54 +02:00
To add a foreign key constraint to a table:
2003-04-15 15:25:08 +02:00
<programlisting>
2011-09-10 15:24:05 +02:00
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
2014-04-06 17:13:43 +02:00
</programlisting>
</para>
<para>
To add a foreign key constraint to a table with the least impact on other work:
<programlisting>
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;
ALTER TABLE distributors VALIDATE CONSTRAINT distfk;
2003-04-15 15:25:08 +02:00
</programlisting>
2000-04-11 16:43:54 +02:00
</para>
2001-09-07 23:57:53 +02:00
2009-02-11 22:11:16 +01:00
<para>
2002-01-20 23:19:57 +01:00
To add a (multicolumn) unique constraint to a table:
2003-04-15 15:25:08 +02:00
<programlisting>
2001-09-12 04:13:25 +02:00
ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
2003-04-15 15:25:08 +02:00
</programlisting>
2002-02-17 12:50:09 +01:00
</para>
2009-02-11 22:11:16 +01:00
<para>
2002-02-17 12:50:09 +01:00
To add an automatically named primary key constraint to a table, noting
that a table can only ever have one primary key:
2003-04-15 15:25:08 +02:00
<programlisting>
2002-02-17 12:50:09 +01:00
ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
2003-04-15 15:25:08 +02:00
</programlisting>
2001-09-07 23:57:53 +02:00
</para>
2004-07-12 01:13:58 +02:00
2009-02-11 22:11:16 +01:00
<para>
2005-08-01 18:11:14 +02:00
To move a table to a different tablespace:
2004-07-12 01:13:58 +02:00
<programlisting>
ALTER TABLE distributors SET TABLESPACE fasttablespace;
2005-08-01 18:11:14 +02:00
</programlisting>
</para>
2009-02-11 22:11:16 +01:00
<para>
2005-08-01 18:11:14 +02:00
To move a table to a different schema:
<programlisting>
ALTER TABLE myschema.distributors SET SCHEMA yourschema;
2004-07-12 01:13:58 +02:00
</programlisting>
</para>
2011-01-25 21:42:03 +01:00
<para>
To recreate a primary key constraint, without blocking updates while the
index is rebuilt:
<programlisting>
2011-01-26 14:35:01 +01:00
CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
2011-01-25 21:42:03 +01:00
ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;
2011-08-07 09:49:45 +02:00
</programlisting></para>
2011-01-25 21:42:03 +01:00
1999-07-06 19:16:42 +02:00
</refsect1>
1998-05-13 07:34:00 +02:00
2003-04-15 15:25:08 +02:00
<refsect1>
<title>Compatibility</title>
1998-09-16 16:43:12 +02:00
2003-04-15 15:25:08 +02:00
<para>
2011-01-25 21:42:03 +01:00
The forms <literal>ADD</literal> (without <literal>USING INDEX</literal>),
<literal>DROP</>, <literal>SET DEFAULT</>,
2008-10-21 10:38:16 +02:00
and <literal>SET DATA TYPE</literal> (without <literal>USING</literal>)
conform with the SQL standard. The other forms are
2003-04-15 15:25:08 +02:00
<productname>PostgreSQL</productname> extensions of the SQL standard.
2004-05-05 06:48:48 +02:00
Also, the ability to specify more than one manipulation in a single
<command>ALTER TABLE</> command is an extension.
2003-04-15 15:25:08 +02:00
</para>
<para>
<command>ALTER TABLE DROP COLUMN</> can be used to drop the only
column of a table, leaving a zero-column table. This is an
extension of SQL, which disallows zero-column tables.
</para>
1999-07-06 19:16:42 +02:00
</refsect1>
2011-01-25 21:42:03 +01:00
<refsect1>
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="sql-createtable"></member>
</simplelist>
</refsect1>
1999-07-06 19:16:42 +02:00
</refentry>