Commit 6d1e3618 authored by Tom Lane's avatar Tom Lane

Change ALTER TABLE SET WITHOUT OIDS to rewrite the whole table to physically

get rid of the OID column.  This eliminates the problem discovered by Heikki
back in November that 8.4's suppression of "unnecessary" junk filtering in
INSERT/SELECT could lead to an Assert failure, or storing of oids into a table
that shouldn't have them if Asserts are off.  While that particular problem
could have been solved in other ways, it seems likely to be just a forerunner
of things to come if we continue to allow tables to contain rows that disagree
with the pg_class.relhasoids setting.  It's better to make this operation slow
than to sacrifice performance or risk bugs in more common code paths.

Also, add ALTER TABLE SET WITH OIDS to rewrite the table to add oids.
This was a bit more controversial, but in view of the very small amount of
extra code needed given the current ALTER TABLE infrastructure, it seems best
to eliminate the asymmetry in features.
parent 68d95f12
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.103 2009/02/09 20:57:59 alvherre Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.104 2009/02/11 21:11:15 tgl Exp $
PostgreSQL documentation
-->
......@@ -52,6 +52,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
ENABLE ALWAYS RULE <replaceable class="PARAMETER">rewrite_rule_name</replaceable>
CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
SET WITHOUT CLUSTER
SET WITH OIDS
SET WITHOUT OIDS
SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
......@@ -185,7 +186,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
<listitem>
<para>
This form adds a new constraint to a table using the same syntax as
<xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
<xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
</para>
</listitem>
</varlistentry>
......@@ -217,10 +218,10 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
The trigger firing mechanism is also affected by the configuration
variable <xref linkend="guc-session-replication-role">. Simply enabled
triggers will fire when the replication role is <quote>origin</>
(the default) or <quote>local</>. Triggers configured <literal>ENABLE REPLICA</literal>
will only fire if the session is in <quote>replica</> mode and triggers
configured <literal>ENABLE ALWAYS</literal> will fire regardless of the current replication
mode.
(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.
</para>
</listitem>
</varlistentry>
......@@ -243,7 +244,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
<term><literal>CLUSTER</literal></term>
<listitem>
<para>
This form selects the default index for future
This form selects the default index for future
<xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
operations. It does not actually re-cluster the table.
</para>
......@@ -262,6 +263,23 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
</listitem>
</varlistentry>
<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>
<varlistentry>
<term><literal>SET WITHOUT OIDS</literal></term>
<listitem>
......@@ -272,12 +290,6 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
except that it will not complain if there is already no
<literal>oid</literal> column.
</para>
<para>
Note that there is no variant of <command>ALTER TABLE</command>
that allows OIDs to be restored to a table once they have been
removed.
</para>
</listitem>
</varlistentry>
......@@ -302,7 +314,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
in the <literal>WITH (<replaceable
class="PARAMETER">storage_parameter</>)</literal> syntax,
<command>ALTER TABLE</> does not treat <literal>OIDS</> as a
storage parameter.
storage parameter. Instead use the <literal>SET WITH OIDS</>
and <literal>SET WITHOUT OIDS</> forms to change OID status.
</para>
</note>
</listitem>
......@@ -373,7 +386,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
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.
See also
See also
<xref linkend="SQL-CREATETABLESPACE" endterm="sql-createtablespace-title">.
</para>
</listitem>
......@@ -637,7 +650,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
Adding a column with a non-null default or changing the type of an
existing column will require the entire table to be rewritten. This
might take a significant amount of time for a large table; and it will
temporarily require double the disk space.
temporarily require double the disk space. Adding or removing a system
<literal>oid</> column likewise requires rewriting the entire table.
</para>
<para>
......@@ -656,9 +670,11 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
the column, but simply makes it invisible to SQL operations. Subsequent
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
reduce the on-disk size of your table, as the space occupied
reduce the on-disk size of your table, as the space occupied
by the dropped column is not reclaimed. The space will be
reclaimed over time as existing rows are updated.
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.)
</para>
<para>
......@@ -806,21 +822,21 @@ ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
</programlisting>
</para>
<para>
<para>
To add a check constraint to a table and all its children:
<programlisting>
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
</programlisting>
</para>
<para>
<para>
To remove a check constraint from a table and all its children:
<programlisting>
ALTER TABLE distributors DROP CONSTRAINT zipchk;
</programlisting>
</para>
<para>
<para>
To remove a check constraint from a table only:
<programlisting>
ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
......@@ -828,21 +844,21 @@ ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
(The check constraint remains in place for any child tables.)
</para>
<para>
<para>
To add a foreign key constraint to a table:
<programlisting>
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
</programlisting>
</para>
<para>
<para>
To add a (multicolumn) unique constraint to a table:
<programlisting>
ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
</programlisting>
</para>
<para>
<para>
To add an automatically named primary key constraint to a table, noting
that a table can only ever have one primary key:
<programlisting>
......@@ -850,14 +866,14 @@ ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
</programlisting>
</para>
<para>
<para>
To move a table to a different tablespace:
<programlisting>
ALTER TABLE distributors SET TABLESPACE fasttablespace;
</programlisting>
</para>
<para>
<para>
To move a table to a different schema:
<programlisting>
ALTER TABLE myschema.distributors SET SCHEMA yourschema;
......
This diff is collapsed.
......@@ -11,7 +11,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.657 2009/02/02 19:31:39 alvherre Exp $
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.658 2009/02/11 21:11:16 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
......@@ -1636,6 +1636,13 @@ alter_table_cmd:
n->behavior = $4;
$$ = (Node *)n;
}
/* ALTER TABLE <name> SET WITH OIDS */
| SET WITH OIDS
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_AddOids;
$$ = (Node *)n;
}
/* ALTER TABLE <name> SET WITHOUT OIDS */
| SET WITHOUT OIDS
{
......
......@@ -13,7 +13,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.390 2009/02/02 19:31:40 alvherre Exp $
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.391 2009/02/11 21:11:16 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -1127,6 +1127,7 @@ typedef enum AlterTableType
AT_ChangeOwner, /* change owner */
AT_ClusterOn, /* CLUSTER ON */
AT_DropCluster, /* SET WITHOUT CLUSTER */
AT_AddOids, /* SET WITH OIDS */
AT_DropOids, /* SET WITHOUT OIDS */
AT_SetTableSpace, /* SET TABLESPACE */
AT_SetRelOptions, /* SET (...) -- AM specific parameters */
......
......@@ -1171,7 +1171,7 @@ NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table c1
drop cascades to table gc1
--
-- Test the ALTER TABLE WITHOUT OIDS command
-- Test the ALTER TABLE SET WITH/WITHOUT OIDS command
--
create table altstartwith (col integer) with oids;
insert into altstartwith values (1);
......@@ -1192,9 +1192,17 @@ select * from altstartwith;
1
(1 row)
-- Run inheritance tests
alter table altstartwith set with oids;
select oid > 0, * from altstartwith;
?column? | col
----------+-----
t | 1
(1 row)
drop table altstartwith;
-- Check inheritance cases
create table altwithoid (col integer) with oids;
-- Inherits parents oid column
-- Inherits parents oid column anyway
create table altinhoid () inherits (altwithoid) without oids;
insert into altinhoid values (1);
select oid > 0, * from altwithoid;
......@@ -1210,7 +1218,6 @@ select oid > 0, * from altinhoid;
(1 row)
alter table altwithoid set without oids;
alter table altinhoid set without oids;
select oid > 0, * from altwithoid; -- fails
ERROR: column "oid" does not exist
LINE 1: select oid > 0, * from altwithoid;
......@@ -1231,6 +1238,63 @@ select * from altinhoid;
1
(1 row)
alter table altwithoid set with oids;
select oid > 0, * from altwithoid;
?column? | col
----------+-----
t | 1
(1 row)
select oid > 0, * from altinhoid;
?column? | col
----------+-----
t | 1
(1 row)
drop table altwithoid cascade;
NOTICE: drop cascades to table altinhoid
create table altwithoid (col integer) without oids;
-- child can have local oid column
create table altinhoid () inherits (altwithoid) with oids;
insert into altinhoid values (1);
select oid > 0, * from altwithoid; -- fails
ERROR: column "oid" does not exist
LINE 1: select oid > 0, * from altwithoid;
^
select oid > 0, * from altinhoid;
?column? | col
----------+-----
t | 1
(1 row)
alter table altwithoid set with oids;
NOTICE: merging definition of column "oid" for child "altinhoid"
select oid > 0, * from altwithoid;
?column? | col
----------+-----
t | 1
(1 row)
select oid > 0, * from altinhoid;
?column? | col
----------+-----
t | 1
(1 row)
-- the child's local definition should remain
alter table altwithoid set without oids;
select oid > 0, * from altwithoid; -- fails
ERROR: column "oid" does not exist
LINE 1: select oid > 0, * from altwithoid;
^
select oid > 0, * from altinhoid;
?column? | col
----------+-----
t | 1
(1 row)
drop table altwithoid cascade;
NOTICE: drop cascades to table altinhoid
-- test renumbering of child-table columns in inherited operations
create table p1 (f1 int);
create table c1 (f2 text, f3 int) inherits (p1);
......
......@@ -921,7 +921,7 @@ order by relname, attnum;
drop table p1, p2 cascade;
--
-- Test the ALTER TABLE WITHOUT OIDS command
-- Test the ALTER TABLE SET WITH/WITHOUT OIDS command
--
create table altstartwith (col integer) with oids;
......@@ -934,10 +934,16 @@ alter table altstartwith set without oids;
select oid > 0, * from altstartwith; -- fails
select * from altstartwith;
-- Run inheritance tests
alter table altstartwith set with oids;
select oid > 0, * from altstartwith;
drop table altstartwith;
-- Check inheritance cases
create table altwithoid (col integer) with oids;
-- Inherits parents oid column
-- Inherits parents oid column anyway
create table altinhoid () inherits (altwithoid) without oids;
insert into altinhoid values (1);
......@@ -946,13 +952,42 @@ select oid > 0, * from altwithoid;
select oid > 0, * from altinhoid;
alter table altwithoid set without oids;
alter table altinhoid set without oids;
select oid > 0, * from altwithoid; -- fails
select oid > 0, * from altinhoid; -- fails
select * from altwithoid;
select * from altinhoid;
alter table altwithoid set with oids;
select oid > 0, * from altwithoid;
select oid > 0, * from altinhoid;
drop table altwithoid cascade;
create table altwithoid (col integer) without oids;
-- child can have local oid column
create table altinhoid () inherits (altwithoid) with oids;
insert into altinhoid values (1);
select oid > 0, * from altwithoid; -- fails
select oid > 0, * from altinhoid;
alter table altwithoid set with oids;
select oid > 0, * from altwithoid;
select oid > 0, * from altinhoid;
-- the child's local definition should remain
alter table altwithoid set without oids;
select oid > 0, * from altwithoid; -- fails
select oid > 0, * from altinhoid;
drop table altwithoid cascade;
-- test renumbering of child-table columns in inherited operations
create table p1 (f1 int);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment