Lists: | pgsql-admin |
---|
From: | "Rob Anderson" <roba(at)bml(dot)uk(dot)com> |
---|---|
To: | <pgsql-admin(at)postgresql(dot)org> |
Subject: | Testing for existence of a record before insert |
Date: | 2004-03-23 16:41:40 |
Message-ID: | FHEPJNONMOLHMPDJDOCMOEPHFGAA.roba@bml.uk.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-admin |
I am trying to find a way of performing something similar to the SQL-Server SQL of
IF NOT EXISTS (SELECT id FROM a_table WHERE id='akey')
THEN INSERT INTO a_table (id, name) values ('akey', 'adetails');
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.619 / Virus Database: 398 - Release Date: 10/03/2004
From: | Bruno Wolff III <bruno(at)wolff(dot)to> |
---|---|
To: | Rob Anderson <roba(at)bml(dot)uk(dot)com> |
Cc: | pgsql-admin(at)postgresql(dot)org |
Subject: | Re: Testing for existence of a record before insert |
Date: | 2004-03-23 17:59:43 |
Message-ID: | 20040323175943.GA26654@wolff.to |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-admin |
On Tue, Mar 23, 2004 at 16:41:40 -0000,
Rob Anderson <roba(at)bml(dot)uk(dot)com> wrote:
> I am trying to find a way of performing something similar to the SQL-Server SQL of
>
> IF NOT EXISTS (SELECT id FROM a_table WHERE id='akey')
> THEN INSERT INTO a_table (id, name) values ('akey', 'adetails');
INSERT INTO a_table (id, name)
SELECT 'akey' as id, 'adetails' as name where not exists
(SELECT id FROM a_table WHERE id='akey')
It is still possible for two transactions to both try to insert the
same value at the same time. You either need to be prepared to handle
the occasional error or you need to get an exclusive lock on a_table.