From: Harpreet Dhaliwal <harpreet.dhaliwal01@gmail.com>
To: Tom Allison <tom@tacocat.net>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Duplicate Unique Key constraint error
Date: Thu, 12 Jul 2007 20:21:10 -0400
Message-ID: <d86a77ef0707121721k37a78e36n6dbc0fdb0dc97517@mail.gmail.com> (raw)
In-Reply-To: <D985E6C3-0D3F-480E-B8F6-459486A05C55@tacocat.net>
References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com>
<469320CA.9080406@shaw.ca>
<d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com>
<d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com>
<5288.1184094554@sss.pgh.pa.us>
<D985E6C3-0D3F-480E-B8F6-459486A05C55@tacocat.net>
How can one rollover a sequence back to zero after you delete records from a
table with one such sequence.
I see it starting with the last value of the sequence inserted.
On 7/11/07, Tom Allison <tom@tacocat.net> wrote:
>
>
> On Jul 10, 2007, at 3:09 PM, Tom Lane wrote:
>
> >
> > "Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.com> writes:
> >> Transaction 1 started, saw max(dig_id) = 30 and inserted new
> >> dig_id=31.
> >> Now the time when Transaction 2 started and read max(dig_id) it
> >> was still 30
> >> and by the time it tried to insert 31, 31 was already inserted by
> >> Transaction 1 and hence the unique key constraint error.
> >
> > This is exactly why you're recommended to use sequences (ie serial
> > columns) for generating IDs. Taking max()+1 does not work, unless
> > you're willing to lock the whole table and throw away vast amounts of
> > concurrency.
>
> I wonder how SQL server is handling this? Are they locking the table?
> I realize it's off-topic, but I'm still curious.
>
> Sequences are your friend. they come in INT and BIGINT flavors, but
> BIGINT is a lot of rows.
>
> Can set set Sequences to automatically rollover back to zero?
>
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: harpreet.dhaliwal01@gmail.com, tom@tacocat.net, pgsql-general@postgresql.org
Subject: Re: Duplicate Unique Key constraint error
In-Reply-To: <d86a77ef0707121721k37a78e36n6dbc0fdb0dc97517@mail.gmail.com>
References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com> <469320CA.9080406@shaw.ca> <d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com> <d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com> <5288.1184094554@sss.pgh.pa.us> <D985E6C3-0D3F-480E-B8F6-459486A05C55@tacocat.net> <d86a77ef0707121721k37a78e36n6dbc0fdb0dc97517@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox