Commit a0bf1a7f authored by Tom Lane's avatar Tom Lane

Fix pg_dump to dump serial columns as serials. Per pghackers discussion,

cause SERIAL column declaration not to imply UNIQUE, so that this can be
done without creating an extra index.
parent 6ebc90b0
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.98 2002/08/13 20:40:43 momjian Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.99 2002/08/19 19:33:33 tgl Exp $
-->
<chapter id="datatype">
......@@ -665,14 +665,17 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
<programlisting>
CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') UNIQUE NOT NULL
<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') NOT NULL
);
</programlisting>
Thus, we have created an integer column and arranged for its default
values to be assigned from a sequence generator. UNIQUE and NOT NULL
constraints are applied to ensure that explicitly-inserted values
will never be duplicates, either.
values to be assigned from a sequence generator. A <literal>NOT NULL</>
constraint is applied to ensure that a NULL value cannot be explicitly
inserted, either. In most cases you would also want to attach a
<literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
duplicate values from being inserted by accident, but this is
not automatic.
</para>
<para>
......@@ -685,20 +688,23 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
</para>
<para>
Implicit sequences supporting the <type>serial</type> types are
not automatically dropped when a table containing a serial type
is dropped. So, the following commands executed in order will likely fail:
<programlisting>
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
DROP TABLE <replaceable class="parameter">tablename</replaceable>;
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
</programlisting>
The sequence will remain in the database until explicitly dropped using
<command>DROP SEQUENCE</command>. (This annoyance will probably be
fixed in some future release.)
The sequence created by a <type>serial</type> type is automatically
dropped when
the owning column is dropped, and cannot be dropped otherwise.
(This was not true in <productname>PostgreSQL</productname> releases
before 7.3. Note that this automatic drop linkage will not occur for a
sequence created by reloading a dump from a pre-7.3 database; the dump
file does not contain the information needed to establish the dependency
link.)
</para>
<note><para>
Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
implied <literal>UNIQUE</literal>. This is no longer automatic. If
you wish a serial column to be <literal>UNIQUE</literal> or a
<literal>PRIMARY KEY</literal> it must now be specified, same as with
any other datatype.
</para></note>
</sect2>
</sect1>
......
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.147 2002/08/18 09:36:25 petere Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.148 2002/08/19 19:33:34 tgl Exp $
-->
<appendix id="release">
......@@ -24,6 +24,7 @@ CDATA means the content is "SGML-free", so you can write without
worries about funny characters.
-->
<literallayout><![CDATA[
SERIAL no longer implies UNIQUE; specify explicitly if index is wanted
pg_dump -n and -N options have been removed. The new behavior is like -n but knows about key words.
CLUSTER is no longer hazardous to your schema
COPY accepts a list of columns to copy
......
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.241 2002/08/19 15:08:47 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.242 2002/08/19 19:33:34 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -876,11 +876,6 @@ transformColumnDefinition(ParseState *pstate, CreateStmtContext *cxt,
constraint->keys = NIL;
column->constraints = lappend(column->constraints, constraint);
constraint = makeNode(Constraint);
constraint->contype = CONSTR_UNIQUE;
constraint->name = NULL; /* assign later */
column->constraints = lappend(column->constraints, constraint);
constraint = makeNode(Constraint);
constraint->contype = CONSTR_NOTNULL;
column->constraints = lappend(column->constraints, constraint);
......@@ -1209,10 +1204,9 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
/*
* Scan the index list and remove any redundant index specifications.
* This can happen if, for instance, the user writes SERIAL PRIMARY
* KEY or SERIAL UNIQUE. A strict reading of SQL92 would suggest
* raising an error instead, but that strikes me as too
* anal-retentive. - tgl 2001-02-14
* This can happen if, for instance, the user writes UNIQUE PRIMARY KEY.
* A strict reading of SQL92 would suggest raising an error instead,
* but that strikes me as too anal-retentive. - tgl 2001-02-14
*
* XXX in ALTER TABLE case, it'd be nice to look for duplicate
* pre-existing indexes, too.
......@@ -1262,7 +1256,7 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
/*
* Finally, select unique names for all not-previously-named indices,
* and display WARNING messages.
* and display NOTICE messages.
*
* XXX in ALTER TABLE case, we fail to consider name collisions against
* pre-existing indexes.
......
This diff is collapsed.
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_dump.h,v 1.96 2002/08/18 09:36:26 petere Exp $
* $Id: pg_dump.h,v 1.97 2002/08/19 19:33:35 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -110,6 +110,9 @@ typedef struct _tableInfo
bool hasoids; /* does it have OIDs? */
int ncheck; /* # of CHECK expressions */
int ntrig; /* # of triggers */
/* these two are set only if table is a SERIAL column's sequence: */
char *owning_tab; /* OID of table owning sequence */
int owning_col; /* attr # of column owning sequence */
bool interesting; /* true if need to collect more data */
bool dump; /* true if we want to dump it */
......@@ -123,12 +126,13 @@ typedef struct _tableInfo
char **atttypnames; /* attribute type names */
int *atttypmod; /* type-specific type modifiers */
int *attstattarget; /* attribute statistics targets */
bool *attisdropped; /* true if attr is dropped; don't dump it */
bool *attisserial; /* true if attr is serial or bigserial */
/*
* Note: we need to store per-attribute notnull and default stuff for
* all interesting tables so that we can tell which constraints were
* inherited.
*/
bool *attisdropped; /* true if attr is dropped; don't dump it */
bool *notnull; /* Not null constraints on attributes */
char **adef_expr; /* DEFAULT expressions */
bool *inhAttrs; /* true if each attribute is inherited */
......
......@@ -6,7 +6,6 @@ CREATE TABLE x (
e text
);
NOTICE: CREATE TABLE will create implicit sequence 'x_a_seq' for SERIAL column 'x.a'
NOTICE: CREATE TABLE / UNIQUE will create implicit index 'x_a_key' for table 'x'
CREATE FUNCTION fn_x_before () RETURNS OPAQUE AS '
BEGIN
NEW.e := ''before trigger fired''::text;
......
......@@ -137,7 +137,6 @@ INSERT INTO iportaltest (i, d, p)
---
CREATE TABLE serialTest (f1 text, f2 serial);
NOTICE: CREATE TABLE will create implicit sequence 'serialtest_f2_seq' for SERIAL column 'serialtest.f2'
NOTICE: CREATE TABLE / UNIQUE will create implicit index 'serialtest_f2_key' for table 'serialtest'
INSERT INTO serialTest VALUES ('foo');
INSERT INTO serialTest VALUES ('bar');
INSERT INTO serialTest VALUES ('force', 100);
......
......@@ -59,11 +59,10 @@ SELECT relname, relhasindex
pg_trigger | t
pg_type | t
road | t
serialtest | t
shighway | t
tenk1 | t
tenk2 | t
(53 rows)
(52 rows)
--
-- another sanity check: every system catalog that has OIDs should have
......
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