Inserting possible dublicate unique keys - Mailing list pgsql-general

From Alvar Freude
Subject Inserting possible dublicate unique keys
Date
Msg-id 3ABF47BC.953AA68@huitzilopochtli
Whole thread Raw
List pgsql-general
Hi,

what is the best method to make concurrent inserts to a table with
unique/primary key?

Scenario:
I write a DBI logger for Apache, and this uses a table for all referers:


CREATE TABLE referer (
   id         SERIAL,
   referer    varchar(2048) NOT NULL PRIMARY KEY
   );


so, you can imagine that there are two accesses with the same referer at
the same time; at logging time, each process looks if there is already
an entry for this referer and catches its id, but if not, it inserts the
new referer.

So, it is possible that two processes trying to insert the same primary
key into the table.


My solution is: if transaction is broken, I restart the hole transaction
(there are more then one inserts like this for each request) a second
time. But i can not be sure that the transaction is aborted because a
dublicate unique key, and it seems to me not the most elegant solution.


Any other ideas?!?


Ciao
  Alvar


--
AGI
Magirusstrasse 21B, 70469 Stuttgart
Fon +49 (0)711.228 74-50, Fax +49 (0)711.228 74-88
+++news+++news+++news+++
Beste Image-Website 2001 kommt von AGI
http://www.agi.de/tagebuch
http://www.agi.com/diary (english)

pgsql-general by date:

Previous
From: "Brett W. McCoy"
Date:
Subject: Re: Starting Postmaster
Next
From: "Brian Baquiran"
Date:
Subject: WAL file lifespan (7.1RC1)