Re: Problems with sequences

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Arturo Perez <aperez(at)hayesinc(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problems with sequences
Date: 2006-09-07 09:35:30
Message-ID: 44FFE7E2.7050806@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)magproductions(dot)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 //

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2006-09-07 09:47:14 Re: Replace NULL values
Previous Message C.Strobl 2006-09-07 06:45:41 postgres and emacs on windows os