Re: duplicate key violates unique on a nextval() field

From: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
To: Peter Warasin <peter(at)endian(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: duplicate key violates unique on a nextval() field
Date: 2011-08-30 16:56:11
Message-ID: 2EB07ECE-7EC9-48C7-9B1C-A308B5CDA90D@elevated-dev.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Aug 30, 2011, at 10:19 AM, Peter Warasin wrote:

> The message tells me furthermore that freeradius tries to insert a
> record with a radacctid which already exists.
>
> But how can that happen when it is bigserial?

Postgres only assigns the value if it is not explicitly provided. Any client, freeradius included, could be assigning ids and could have bugs. Allowing pg to assign the value is safe, using nextval is safe--I'd look for client code that tries to get ranges ahead of time & cache...

> The error message posted above tells me that the insert statement does
> even not succeed after the reconnect. (failed after reconnect)

If freeradius is trying to insert a record with an id that already exists, after a re-connect there's no reason at all to assume that the previously existing record is gone and that the insert with the same id will now succeed.

That DDL is also kind of nasty... Why the big effort to set the sequence to 1 immediately after creating the table? Why the creation of a unique index when the "primary key" attribute already causes a unique index to be created on the id? Ugh.

--
Scott Ribe
scott_ribe(at)elevated-dev(dot)com
http://www.elevated-dev.com/
(303) 722-0567 voice

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2011-08-30 17:13:51 Re: IDLE queries taking up space
Previous Message Peter Warasin 2011-08-30 16:19:47 duplicate key violates unique on a nextval() field