Inserting possible dublicate unique keys

From: Alvar Freude <alvar(dot)freude(at)huitzilopochtli(dot)agi(dot)de>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Inserting possible dublicate unique keys
Date: 2001-03-26 13:44:28
Message-ID: 3ABF47BC.953AA68@huitzilopochtli
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Hartmeier 2001-03-26 14:14:51 Integrity violation when adding foreign key constraint
Previous Message Zuev Dmitry 2001-03-26 12:13:16 TRANSACTION error question