Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a0bf1a7

Browse files
committed
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.
1 parent 6ebc90b commit a0bf1a7

File tree

8 files changed

+249
-100
lines changed

8 files changed

+249
-100
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.98 2002/08/13 20:40:43 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.99 2002/08/19 19:33:33 tgl Exp $
33
-->
44

55
<chapter id="datatype">
@@ -665,14 +665,17 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
665665
<programlisting>
666666
CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
667667
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
668-
<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') UNIQUE NOT NULL
668+
<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') NOT NULL
669669
);
670670
</programlisting>
671671

672672
Thus, we have created an integer column and arranged for its default
673-
values to be assigned from a sequence generator. UNIQUE and NOT NULL
674-
constraints are applied to ensure that explicitly-inserted values
675-
will never be duplicates, either.
673+
values to be assigned from a sequence generator. A <literal>NOT NULL</>
674+
constraint is applied to ensure that a NULL value cannot be explicitly
675+
inserted, either. In most cases you would also want to attach a
676+
<literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
677+
duplicate values from being inserted by accident, but this is
678+
not automatic.
676679
</para>
677680

678681
<para>
@@ -685,20 +688,23 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
685688
</para>
686689

687690
<para>
688-
Implicit sequences supporting the <type>serial</type> types are
689-
not automatically dropped when a table containing a serial type
690-
is dropped. So, the following commands executed in order will likely fail:
691-
692-
<programlisting>
693-
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
694-
DROP TABLE <replaceable class="parameter">tablename</replaceable>;
695-
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
696-
</programlisting>
697-
698-
The sequence will remain in the database until explicitly dropped using
699-
<command>DROP SEQUENCE</command>. (This annoyance will probably be
700-
fixed in some future release.)
691+
The sequence created by a <type>serial</type> type is automatically
692+
dropped when
693+
the owning column is dropped, and cannot be dropped otherwise.
694+
(This was not true in <productname>PostgreSQL</productname> releases
695+
before 7.3. Note that this automatic drop linkage will not occur for a
696+
sequence created by reloading a dump from a pre-7.3 database; the dump
697+
file does not contain the information needed to establish the dependency
698+
link.)
701699
</para>
700+
701+
<note><para>
702+
Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
703+
implied <literal>UNIQUE</literal>. This is no longer automatic. If
704+
you wish a serial column to be <literal>UNIQUE</literal> or a
705+
<literal>PRIMARY KEY</literal> it must now be specified, same as with
706+
any other datatype.
707+
</para></note>
702708
</sect2>
703709
</sect1>
704710

doc/src/sgml/release.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.147 2002/08/18 09:36:25 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.148 2002/08/19 19:33:34 tgl Exp $
33
-->
44

55
<appendix id="release">
@@ -24,6 +24,7 @@ CDATA means the content is "SGML-free", so you can write without
2424
worries about funny characters.
2525
-->
2626
<literallayout><![CDATA[
27+
SERIAL no longer implies UNIQUE; specify explicitly if index is wanted
2728
pg_dump -n and -N options have been removed. The new behavior is like -n but knows about key words.
2829
CLUSTER is no longer hazardous to your schema
2930
COPY accepts a list of columns to copy

src/backend/parser/analyze.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.241 2002/08/19 15:08:47 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.242 2002/08/19 19:33:34 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -876,11 +876,6 @@ transformColumnDefinition(ParseState *pstate, CreateStmtContext *cxt,
876876
constraint->keys = NIL;
877877
column->constraints = lappend(column->constraints, constraint);
878878

879-
constraint = makeNode(Constraint);
880-
constraint->contype = CONSTR_UNIQUE;
881-
constraint->name = NULL; /* assign later */
882-
column->constraints = lappend(column->constraints, constraint);
883-
884879
constraint = makeNode(Constraint);
885880
constraint->contype = CONSTR_NOTNULL;
886881
column->constraints = lappend(column->constraints, constraint);
@@ -1209,10 +1204,9 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
12091204

12101205
/*
12111206
* Scan the index list and remove any redundant index specifications.
1212-
* This can happen if, for instance, the user writes SERIAL PRIMARY
1213-
* KEY or SERIAL UNIQUE. A strict reading of SQL92 would suggest
1214-
* raising an error instead, but that strikes me as too
1215-
* anal-retentive. - tgl 2001-02-14
1207+
* This can happen if, for instance, the user writes UNIQUE PRIMARY KEY.
1208+
* A strict reading of SQL92 would suggest raising an error instead,
1209+
* but that strikes me as too anal-retentive. - tgl 2001-02-14
12161210
*
12171211
* XXX in ALTER TABLE case, it'd be nice to look for duplicate
12181212
* pre-existing indexes, too.
@@ -1262,7 +1256,7 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
12621256

12631257
/*
12641258
* Finally, select unique names for all not-previously-named indices,
1265-
* and display WARNING messages.
1259+
* and display NOTICE messages.
12661260
*
12671261
* XXX in ALTER TABLE case, we fail to consider name collisions against
12681262
* pre-existing indexes.

0 commit comments

Comments
 (0)