pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Glaesemann <grzm@seespotcode.net>
To: Harpreet Dhaliwal <harpreet.dhaliwal01@gmail.com>
Cc: Ron St-Pierre <ron.pgsql@shaw.ca>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Duplicate Unique Key constraint error
Date: Tue, 10 Jul 2007 13:35:16 -0500
Message-ID: <070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net> (raw)
In-Reply-To: <d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com>
References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com>
	<469320CA.9080406@shaw.ca>
	<d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com>
	<d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com>


On Jul 10, 2007, at 13:22 , Harpreet Dhaliwal wrote:

> 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.

Why would the server lock the table? It can't know your intention is  
to add one to the number returned and insert. If this is what you  
want, you have to lock the table explicitly.

> Please guide me throug to get rid of this problem.

This exact reason is why sequences are often used for primary keys. I  
recommend you change your primary key.


Michael Glaesemann
grzm seespotcode net





view thread (10+ messages)  latest in thread

Message-ID: <070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net>
Permalink:  ../070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net/
Also on:    postgresql.org/message-id/070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: grzm@seespotcode.net, harpreet.dhaliwal01@gmail.com, ron.pgsql@shaw.ca, pgsql-general@postgresql.org
  Subject: Re: Duplicate Unique Key constraint error
  In-Reply-To: <070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net>
  References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com> <469320CA.9080406@shaw.ca> <d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com> <d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com> <070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox