Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 937869FBC3C for ; Tue, 10 Jul 2007 03:04:02 -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 64009-03 for ; Tue, 10 Jul 2007 03:03:42 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from pd2mo3so.prod.shaw.ca (shawidc-mo1.cg.shawcable.net [24.71.223.10]) by postgresql.org (Postfix) with ESMTP id B0A9E9FB631 for ; Tue, 10 Jul 2007 03:03:42 -0300 (ADT) Received: from pd3mr1so.prod.shaw.ca (pd3mr1so-qfe3.prod.shaw.ca [10.0.141.177]) by l-daemon (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0JKY00N2C8TA31C0@l-daemon> for pgsql-general@postgresql.org; Tue, 10 Jul 2007 00:03:10 -0600 (MDT) Received: from pn2ml10so.prod.shaw.ca ([10.0.121.80]) by pd3mr1so.prod.shaw.ca (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0JKY00KTT8T8TVB0@pd3mr1so.prod.shaw.ca> for pgsql-general@postgresql.org; Tue, 10 Jul 2007 00:03:09 -0600 (MDT) Received: from [192.168.0.100] ([24.68.207.184]) by l-daemon (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0JKY00L0U8T7KBF1@l-daemon> for pgsql-general@postgresql.org; Tue, 10 Jul 2007 00:03:08 -0600 (MDT) Date: Mon, 09 Jul 2007 23:01:46 -0700 From: Ron St-Pierre Subject: Re: Duplicate Unique Key constraint error In-reply-to: To: Harpreet Dhaliwal Cc: Postgres General Message-id: <469320CA.9080406@shaw.ca> MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7bit References: User-Agent: Thunderbird 1.5.0.12 (Windows/20070509) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/381 X-Sequence-Number: 116982 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