Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id B8EF79FB400 for ; Thu, 12 Jul 2007 21:21:19 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 84557-03 for ; Thu, 12 Jul 2007 21:21:05 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.186]) by postgresql.org (Postfix) with ESMTP id B64BF9FB464 for ; Thu, 12 Jul 2007 21:21:15 -0300 (ADT) Received: by nf-out-0910.google.com with SMTP id f5so23261nfh for ; Thu, 12 Jul 2007 17:21:10 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=ZUPmCkN2cR7cA5bxEu6lzyQkSvH4p9vbL3OkkTq8Rh4qAT8m/OXKr6UDuURCTMDJK0wPwEg2usCiKehUmuAF+4prPkuyxbN9TcwdCHvaqhVElTWIFmBi0DagLtM38ZZr6xIXDr0OMRNlu0I3IRTAzdxUFNgi84OBUXJnFYzExzQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=lEXySdN5rn7gQdybulbrnS5jBXxEEn7jS9OAiuihdUaCBeMzRlZ8LiQQCo5KDBrO68Th+veJ9c0NrkmW8B2/deGQfWqV+GB8KRKYeOee4T0+2sUpsrTfv8UkMR1g6Lslo2RM3Kr0aJUoKrOKC1ncz4COdPkyaYoimR++VXsTvZc= Received: by 10.82.112.3 with SMTP id k3mr1446687buc.1184286070398; Thu, 12 Jul 2007 17:21:10 -0700 (PDT) Received: by 10.82.188.18 with HTTP; Thu, 12 Jul 2007 17:21:10 -0700 (PDT) Message-ID: Date: Thu, 12 Jul 2007 20:21:10 -0400 From: "Harpreet Dhaliwal" To: "Tom Allison" Subject: Re: Duplicate Unique Key constraint error Cc: "Postgres General" In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_23120_14116643.1184286070365" References: <469320CA.9080406@shaw.ca> <5288.1184094554@sss.pgh.pa.us> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/528 X-Sequence-Number: 117129 ------=_Part_23120_14116643.1184286070365 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 wrote: > > > On Jul 10, 2007, at 3:09 PM, Tom Lane wrote: > > > > > "Harpreet Dhaliwal" 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? > > ------=_Part_23120_14116643.1184286070365 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
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?


------=_Part_23120_14116643.1184286070365--