pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Rob Anderson <roba@bml.uk.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Testing for existence of a record before insert
Date: Tue, 23 Mar 2004 11:59:43 -0600
Message-ID: <20040323175943.GA26654@wolff.to> (raw)
In-Reply-To: <FHEPJNONMOLHMPDJDOCMOEPHFGAA.roba@bml.uk.com>
References: <FHEPJNONMOLHMPDJDOCMOEPHFGAA.roba@bml.uk.com>

On Tue, Mar 23, 2004 at 16:41:40 -0000,
  Rob Anderson <roba@bml.uk.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.



view thread (2+ messages)

Message-ID: <20040323175943.GA26654@wolff.to>
Permalink:  ../20040323175943.GA26654@wolff.to/
Also on:    postgresql.org/message-id/20040323175943.GA26654@wolff.to

 · 

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-admin@postgresql.org
  Cc: bruno@wolff.to, roba@bml.uk.com
  Subject: Re: Testing for existence of a record before insert
  In-Reply-To: <20040323175943.GA26654@wolff.to>
  References: <FHEPJNONMOLHMPDJDOCMOEPHFGAA.roba@bml.uk.com> <20040323175943.GA26654@wolff.to>

* 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