pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
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 15:09:14 -0400
Message-ID: <5288.1184094554@sss.pgh.pa.us> (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>

"Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.com> writes:
> 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.

This is exactly why you're recommended to use sequences (ie serial
columns) for generating IDs.  Taking max()+1 does not work, unless
you're willing to lock the whole table and throw away vast amounts of
concurrency.

			regards, tom lane



view thread (10+ messages)  latest in thread

Message-ID: <5288.1184094554@sss.pgh.pa.us>
Permalink:  ../5288.1184094554@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5288.1184094554@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, harpreet.dhaliwal01@gmail.com, ron.pgsql@shaw.ca, pgsql-general@postgresql.org
  Subject: Re: Duplicate Unique Key constraint error
  In-Reply-To: <5288.1184094554@sss.pgh.pa.us>
  References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com> <469320CA.9080406@shaw.ca> <d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com> <d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com> <5288.1184094554@sss.pgh.pa.us>

* 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