Re: Cannot insert a duplicate key into unique index

Lists: pgsql-novice
From: <kynn(at)panix(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Cannot insert a duplicate key into unique index
Date: 2004-02-19 04:06:11
Message-ID: 200402190406.i1J46BP26112@panix3.panix.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

I'm trying to debug some problem in my database that is resulting in
an error of the form "Cannot insert a duplicate key into unique
index". The insert statement that is producing this error does not
include a value for the pkey field in question (this field is of type
SERIAL). I imagine that somehow the counter associated with this
field got messed up, so that it is mistakenly generating a value that
has been used already. How can I straighten it out?

Thanks!

kj


From: Noel <noel(dot)faux(at)med(dot)monash(dot)edu(dot)au>
To: kynn(at)panix(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 04:18:45
Message-ID: 40343925.7010205@med.monash.edu.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Hi KJ,

For each SERIAL primary key, there is a sequence table for that table,
which is where the primary key is generated. You need to check to see
that the current counter in the sequence table is passed the MAX primary
key in your table.
eg:
TABLE A
id (pkey)

Will have a sequence table
A_id_seq

For further information look at:
http://www.au.postgresql.org/docs/7.4/static/datatype.html#DATATYPE-SERIAL
and
http://www.postgresql.org/docs/7.4/static/functions-sequence.html

Hope that helps
Cheers
Noel

kynn(at)panix(dot)com wrote:

>I'm trying to debug some problem in my database that is resulting in
>an error of the form "Cannot insert a duplicate key into unique
>index". The insert statement that is producing this error does not
>include a value for the pkey field in question (this field is of type
>SERIAL). I imagine that somehow the counter associated with this
>field got messed up, so that it is mistakenly generating a value that
>has been used already. How can I straighten it out?
>
>Thanks!
>
>kj
>
>---------------------------(end of broadcast)---------------------------
>TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match
>
>
>

--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: kynn(at)panix(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 05:10:39
Message-ID: 28971.1077167439@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

<kynn(at)panix(dot)com> writes:
> The insert statement that is producing this error does not
> include a value for the pkey field in question (this field is of type
> SERIAL). I imagine that somehow the counter associated with this
> field got messed up, so that it is mistakenly generating a value that
> has been used already. How can I straighten it out?

You need to do something like

select setval('seq-name', (select max(col) + 1 from table));

regards, tom lane


From: daq <daq(at)ugyvitelszolgaltato(dot)hu>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 07:01:04
Message-ID: 17675789710.20040219080104@ugyvitelszolgaltato.hu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice


>> The insert statement that is producing this error does not
>> include a value for the pkey field in question (this field is of type
>> SERIAL). I imagine that somehow the counter associated with this
>> field got messed up, so that it is mistakenly generating a value that
>> has been used already. How can I straighten it out?

TL> You need to do something like

TL> select setval('seq-name', (select max(col) + 1 from table));

TL> regards, tom lane

The setval function updates the last_value field of the sequence table. You don't need the "+ 1".

select setval('seq-name', (select max(col) from table));

DAQ


From: <kynn(at)panix(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 12:03:25
Message-ID: 200402191203.i1JC3Pq02831@panix3.panix.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Date: Thu, 19 Feb 2004 08:01:04 +0100
From: daq <daq(at)ugyvitelszolgaltato(dot)hu>

The setval function updates the last_value field of the sequence table. You don't need the "+ 1".

select setval('seq-name', (select max(col) from table));

Thank you all for the help. I'm almost there.

How can I list all the sequences in the database, with their
attributes (such as last_value)? (I'm having a hard time guessing
'seq-name'; the 'A_id_seq' formula did not work.)

For that matter (going beyond my original question) does PostgreSQL
have anything like a comprehensive "catalog" function that will list
all the tables, their fields, etc. defined in the database?

Thanks!

kj


From: daq <daq(at)ugyvitelszolgaltato(dot)hu>
To: kynn(at)panix(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 14:12:09
Message-ID: 89101658505.20040219151209@ugyvitelszolgaltato.hu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice


kpc> Thank you all for the help. I'm almost there.

kpc> How can I list all the sequences in the database, with their
kpc> attributes (such as last_value)? (I'm having a hard time guessing
kpc> 'seq-name'; the 'A_id_seq' formula did not work.)

kpc> For that matter (going beyond my original question) does PostgreSQL
kpc> have anything like a comprehensive "catalog" function that will list
kpc> all the tables, their fields, etc. defined in the database?

You can list all the sequences:

select relname from pg_class where relkind='S';

and list all their atributes:

select * from sequence_name;

See "System Catalogs" in the documentation!

DAQ


From: Terry Lee Tucker <terry(at)esc1(dot)com>
To: daq <daq(at)ugyvitelszolgaltato(dot)hu>, kynn(at)panix(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 14:33:02
Message-ID: 200402190933.02767.terry@esc1.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

It's amazing how much good information comes accross this list. Thanks for the
knowledge.

On Thursday 19 February 2004 09:12 am, daq saith:
>
>
> You can list all the sequences:
>
> select relname from pg_class where relkind='S';
>
> and list all their atributes:
>
> select * from sequence_name;
>
> See "System Catalogs" in the documentation!
>
> DAQ
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly

--
Quote: 3
"There is a rank due to the United States, among nations, which will be
withheld, if not absolutely lost, by the reputation of weakness. If we
desire to avoid insult, we must be able to repel it; if we desire to
secure peace, one of the most powerful instruments of our rising prosperity,
it must be known that we are at all times ready for war."

--George Washington

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry(at)esc1(dot)com


From: <kynn(at)panix(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 14:43:17
Message-ID: 200402191443.i1JEhHt18578@panix3.panix.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice


X-Original-To: kynn(at)panix(dot)com
Date: Thu, 19 Feb 2004 15:12:09 +0100
From: daq <daq(at)ugyvitelszolgaltato(dot)hu>

kpc> Thank you all for the help. I'm almost there.

kpc> How can I list all the sequences in the database, with their
kpc> attributes (such as last_value)? (I'm having a hard time guessing
kpc> 'seq-name'; the 'A_id_seq' formula did not work.)

kpc> For that matter (going beyond my original question) does PostgreSQL
kpc> have anything like a comprehensive "catalog" function that will list
kpc> all the tables, their fields, etc. defined in the database?

You can list all the sequences:

select relname from pg_class where relkind='S';

and list all their atributes:

select * from sequence_name;

See "System Catalogs" in the documentation!

Way cool. Thanks.

BTW, I was wrong when I said that the 'A_id_seq' formula did not work
(it turns out that it failed due to a typo of mine).

kj


From: joseph speigle <joe(dot)speigle(at)jklh(dot)us>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-02-19 15:10:21
Message-ID: 20040219151021.GA689@www.sirfsup.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

>
> kpc> How can I list all the sequences in the database, with their
> kpc> attributes (such as last_value)? (I'm having a hard time guessing
> kpc> 'seq-name'; the 'A_id_seq' formula did not work.)
>
> You can list all the sequences:
>
> select relname from pg_class where relkind='S';
>
> and list all their atributes:
>
> select * from sequence_name;
>

select last_value from something_seq

And I got the sequence name from \ds at the psql prompt! but that's only for the current database.

joe
--
speigle
www.sirfsup.com


From: joseph speigle <joe(dot)speigle(at)jklh(dot)us>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Cannot insert a duplicate key into unique index
Date: 2004-06-04 03:40:43
Message-ID: 20040604034043.GA18767@www.sirfsup.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

hi kj,

select nextval('shipments_ship_id_seq');
then add one to that, say it's now equal 1010 ...
SELECT setval('shipments_ship_id_seq', 1010);

source =

http://www.commandprompt.com/ppbook/index.lxp?lxpwrap=x14316%2ehtm#SETTINGASEQUENCEVALUE

but, sometimes there are deeper problems, as you alluded to.

On Thu, Feb 19, 2004 at 03:18:45PM +1100, Noel wrote:
> Hi KJ,
>
> For each SERIAL primary key, there is a sequence table for that table,
> which is where the primary key is generated. You need to check to see
> that the current counter in the sequence table is passed the MAX primary
> key in your table.
> eg:
> TABLE A
> id (pkey)
>
> Will have a sequence table
> A_id_seq
>
> Hope that helps
> Cheers
> Noel
>
> kynn(at)panix(dot)com wrote:
>
> >I'm trying to debug some problem in my database that is resulting in
> >an error of the form "Cannot insert a duplicate key into unique
> >index". The insert statement that is producing this error does not
> >include a value for the pkey field in question (this field is of type
> >SERIAL). I imagine that somehow the counter associated with this
> >field got messed up, so that it is mistakenly generating a value that
> >has been used already. How can I straighten it out?
> >
> >Thanks!
> >
> >kj
--
joe speigle
www.sirfsup.com