Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 399269FBE4D for ; Tue, 10 Jul 2007 15:23:13 -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 17907-04-4 for ; Tue, 10 Jul 2007 15:22:54 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.190]) by postgresql.org (Postfix) with ESMTP id 92AB39FBE60 for ; Tue, 10 Jul 2007 15:22:49 -0300 (ADT) Received: by mu-out-0910.google.com with SMTP id i10so1534597mue for ; Tue, 10 Jul 2007 11:22:47 -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=Xv8UOzRs/NFSb3Ln1ljyV2pPg6ifcsMvgie0cCiinpFdaGla19JhCDTN9CpGqeEnZ3OG6xrHqOMqg3uqvGeuElZR1EPGdDVGkdsmeZnkTxXxQCdRNf/YeLMJ3RBt43MTgG/A26Mgjc2YBzqt3f5AN73Um3y9aaF5zUDYVS2uJwg= 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=qjRWGIon/8H8gJPwUM37wRZ+ig3oxBX+k5UG0ae3L5rmcdD6V24UXXXRTUUBkR2KD1LO4u1X42L5XDMIBu8nAP3ywHpMUkX10gM6C3x+s1zJC49WJIjU0YOcGznzC8aycU0e5Z9eSfuYOhulGdUzDoAwutq2uXWlvHsa5KdkKwE= Received: by 10.82.126.5 with SMTP id y5mr10013940buc.1184091767424; Tue, 10 Jul 2007 11:22:47 -0700 (PDT) Received: by 10.82.188.18 with HTTP; Tue, 10 Jul 2007 11:22:47 -0700 (PDT) Message-ID: Date: Tue, 10 Jul 2007 14:22:47 -0400 From: "Harpreet Dhaliwal" To: "Ron St-Pierre" Subject: Re: Duplicate Unique Key constraint error Cc: "Postgres General" In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_166359_26854691.1184091767364" References: <469320CA.9080406@shaw.ca> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/433 X-Sequence-Number: 117034 ------=_Part_166359_26854691.1184091767364 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I lately figured out the actual problem PHEW. Its something like two different transactions are seeing the same snapshot of the database. 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. I thought this would be taken care by the database itself by locking the transactions but now I really don't know how does this locking takes place in postgres. I used to work with SQL Server and never faced this problem there. Please guide me throug to get rid of this problem. Thanks, ~Harpreet On 7/10/07, Harpreet Dhaliwal wrote: > > my primary key is neither SERIAL nor a SEQUENCE. > > CONSTRAINT pk_dig PRIMARY KEY (dig_id) > > This is the clause that I have for my primary key in the create table > script. > > thanks, > ~Harpreet > > On 7/10/07, Ron St-Pierre wrote: > > > > Harpreet Dhaliwal wrote: > > > Hi, > > > > > > I keep getting this duplicate unique key constraint error for my > > > primary key even > > > though I'm not inserting anything duplicate. It even inserts the > > > records properly > > > but my console throws this error that I'm sure of what it is all > > about. > > > > > > Corruption of my Primary Key can be one of the possibilities but I'm > > > really not sure how > > > to get rid of this corruption and how to re-index the primary key. > > > > > > Also, I was wondering what could be the cause of this PK corruption, > > > if possible and what does can this corruption lead to. > > > I mean what are its cons. > > > > > > Thanks, > > > ~Harpreet > > You haven't really given any useful information about your primary key, > > but if you are using SERIAL as the column type (INT type with a > > sequence) you may just be having a problem with its current value (but > > then inserts shouldn't work). > > > > If you are using a sequence here, see what it's current value is and > > compare it to the highest value in the column. If its value is less than > > > > the columns max() value, just reset the value in the sequence. > > > > imp=# CREATE TABLE dup_pkey (id SERIAL PRIMARY KEY, insert_order int); > > imp=# INSERT INTO dup_pkey (insert_order) VALUES (1); > > imp=# INSERT INTO dup_pkey (insert_order) VALUES (2); > > imp=# INSERT INTO dup_pkey (insert_order) VALUES (3); > > imp=# INSERT INTO dup_pkey (insert_order) VALUES (4); > > > > imp=# SELECT * FROM dup_pkey; > > id | insert_order > > ----+-------------- > > 1 | 1 > > 2 | 2 > > 3 | 3 > > 4 | 4 > > (4 rows) > > > > Now, if you set the value below what the max() column value is, you will > > have a problem with inserts. > > imp=# SELECT setval('dup_pkey_id_seq',3); > > setval > > -------- > > 3 > > (1 row) > > > > imp=# INSERT INTO dup_pkey (insert_order) VALUES (5); > > ERROR: duplicate key violates unique constraint "dup_pkey_pkey" > > > > > > If this is the case, use setval() to update the value of the sequence to > > > > the max() value of your primary key. You can use \d to get information > > about your table, including the sequence name. However if, as you say, > > it IS inserting records properly, then this ISN'T going to help. > > > > hth > > > > Ron > > > > ------=_Part_166359_26854691.1184091767364 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline I lately figured out the actual problem PHEW.
Its something like two different transactions are seeing the same snapshot of the database.

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.

I thought this would be taken care by the database itself by locking the transactions but now I really don't know how does this locking takes place in postgres. I used to work with SQL Server and never faced this problem there.

Please guide me throug to get rid of this problem.

Thanks,
~Harpreet

On 7/10/07, Harpreet Dhaliwal < harpreet.dhaliwal01@gmail.com> wrote:
my primary key is neither SERIAL nor a SEQUENCE.

CONSTRAINT pk_dig PRIMARY KEY (dig_id)

This is the clause that I have for my primary key in the create table script.

thanks,
~Harpreet

On 7/10/07, Ron St-Pierre < ron.pgsql@shaw.ca> wrote:
Harpreet Dhaliwal wrote:
> Hi,
>
> I keep getting this duplicate unique key constraint error for my
> primary key even
> though I'm not inserting anything duplicate. It even inserts the
> records properly
> but my console throws this error that I'm sure of what it is all about.
>
> Corruption of my Primary Key can be one of the possibilities but I'm
> really not sure how
> to get rid of this corruption and how to re-index the primary key.
>
> Also, I was wondering what could be the cause of this PK  corruption,
> if possible and what does can this corruption lead to.
> I mean what are its cons.
>
> Thanks,
> ~Harpreet
You haven't really given any useful information about your primary key,
but if you are using SERIAL as the column type (INT type with a
sequence) you may just be having a problem with its current value (but
then inserts shouldn't work).

If you are using a sequence here, see what it's current value is and
compare it to the highest value in the column. If its value is less than
the columns max() value, just reset the value in the sequence.

imp=# CREATE TABLE dup_pkey (id SERIAL PRIMARY KEY, insert_order int);
imp=# INSERT INTO dup_pkey (insert_order) VALUES (1);
imp=# INSERT INTO dup_pkey (insert_order) VALUES (2);
imp=# INSERT INTO dup_pkey (insert_order) VALUES (3);
imp=# INSERT INTO dup_pkey (insert_order) VALUES (4);

imp=# SELECT * FROM dup_pkey;
id | insert_order
----+--------------
  1 |            1
  2 |            2
  3 |            3
  4 |            4
(4 rows)

Now, if you set the value below what the max() column value is, you will
have a problem with inserts.
imp=# SELECT setval('dup_pkey_id_seq',3);
setval
--------
      3
(1 row)

imp=# INSERT INTO dup_pkey (insert_order) VALUES (5);
ERROR:  duplicate key violates unique constraint "dup_pkey_pkey"


If this is the case, use setval() to update the value of the sequence to
the max() value of your primary key. You can use \d to get information
about your table, including the sequence name. However if, as you say,
it IS inserting records properly, then this ISN'T going to help.

hth

Ron


------=_Part_166359_26854691.1184091767364--