Re: Problems with sequences - Mailing list pgsql-general

From Alban Hertroys
Subject Re: Problems with sequences
Date
Msg-id 44FFE7E2.7050806@magproductions.nl
Whole thread Raw
In response to Problems with sequences  ("Arturo Perez" <aperez@hayesinc.com>)
Responses Re: Problems with sequences
List pgsql-general
Arturo Perez wrote:
> What happens is that if I do a select nextval('seq') I get a number
> that's lower than the
> max primary key id.  This is inspite of my doing
>    SELECT setval('seq', ((SELECT MAX(seq_ID) FROM table)+1))
>    ALTER SEQUENCE seq RESTART WITH <max + 1>;
>    select pg_catalog.setval(seq, <max+1>, true);

This seems to be a bit over the top;
    SELECT setval('seq', (SELECT MAX(seq_ID) FROM table)
should be enough. Even the +1 isn't necessary, as the first value the
sequence will return is already 1 higher than the value retrieved from MAX.

Are you sure you're using the correct sequence(s) to retrieve your
column values for the problematic table(s)? How do you set the values
for seqID?

Also note that a SERIAL type column is simply a macro for creating an
INT4 type column with a DEFAULT nextval('...'). You can easily change
your schema to include the defaults.

--
Alban Hertroys
alban@magproductions.nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
    7500 AK Enschede

// Integrate Your World //

pgsql-general by date:

Previous
From: Michelle Konzack
Date:
Subject: Re: Tsearch2 & Hebrew
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Replace NULL values