pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lincoln Yeoh <lyeoh@pop.jaring.my>
To: jhondius@rem.nl
To: Pau Marc Muñoz Torres <paumarc@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: conditional insert
Date: Wed, 07 Sep 2011 02:50:29 +0800
Message-ID: <20110906190548.433E8B5DBD1@mail.postgresql.org> (raw)
In-Reply-To: <4E64AC45.6030704@rem.nl>
References: <CADFuJLjCF6fmxswgS033EFyofmbJFy+J0ToUvwTCDDLYj+nr3Q@mail.gmail.com>
	<j4261h$lj8$1@dough.gmane.org>
	<CADFuJLi3K0X1A7OwR+9_neWjKCEXtYYyMP_n=x5z=D2+GNCTNw@mail.gmail.com>
	<4E64A79E.7030503@compulab.co.il>
	<CADFuJLjfHUMEuYTPKgG6GtJMfzJeeq3-bkt162RywRVqc7OYgg@mail.gmail.com>
	<4E64AC45.6030704@rem.nl>

At 07:02 PM 9/5/2011, J. Hondius wrote:
>I agree that there are better ways to do this.
>But for me this works. (legacy driven situation)
>
>INSERT INTO tbinitialisatie (col1, col2)
>  SELECT 'x', 'y'
>  FROM tbinitialisatie
>  WHERE not exists (select * from tbinitialisatie where col1 = 'x' 
> and col2 = 'y')
>  LIMIT 1

Hi,

That does not work 100%. Try it with two psql instances.

Do:
*** psql #1
begin;
INSERT INTO tbinitialisatie (col1, col2)
  SELECT 'x', 'y'
  FROM tbinitialisatie
  WHERE not exists (select * from tbinitialisatie where col1 = 'x' 
and col2 = 'y')
  LIMIT 1 ;

*** psql #2
begin;
INSERT INTO tbinitialisatie (col1, col2)
  SELECT 'x', 'y'
  FROM tbinitialisatie
  WHERE not exists (select * from tbinitialisatie where col1 = 'x' 
and col2 = 'y')
  LIMIT 1 ;
commit;

*** psql #1
commit;

You should find duplicate inserts.

In most cases the "begin" and "commit" are very close together so you 
won't notice the problem. But one day you might get unlucky.

Your options are:
a) put a unique constraint and handle the insert errors when they occur
b) lock the entire table first (affects performance: blocks all 
selects on that table)
c) use a lock elsewhere (but this requires all applications using the 
database to cooperate and use the lock).
d) wait for SQL MERGE to be implemented ( but from what I see the 
current proposal seems to require a) or b) anyway: 
http://wiki.postgresql.org/wiki/SQL_MERGE )

You could do both a) and b) too. Or both a) and c) (if you don't want 
insert errors in the cooperating apps and want to allow other selects 
during the transaction).

Regards,
Link.




view thread (36+ messages)  latest in thread

Message-ID: <20110906190548.433E8B5DBD1@mail.postgresql.org>
Permalink:  ../20110906190548.433E8B5DBD1@mail.postgresql.org/
Also on:    postgresql.org/message-id/20110906190548.433E8B5DBD1@mail.postgresql.org

 · 

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-general@postgresql.org
  Cc: lyeoh@pop.jaring.my, jhondius@rem.nl, paumarc@gmail.com
  Subject: Re: conditional insert
  In-Reply-To: <20110906190548.433E8B5DBD1@mail.postgresql.org>
  References: <CADFuJLjCF6fmxswgS033EFyofmbJFy+J0ToUvwTCDDLYj+nr3Q@mail.gmail.com> <j4261h$lj8$1@dough.gmane.org> <CADFuJLi3K0X1A7OwR+9_neWjKCEXtYYyMP_n=x5z=D2+GNCTNw@mail.gmail.com> <4E64A79E.7030503@compulab.co.il> <CADFuJLjfHUMEuYTPKgG6GtJMfzJeeq3-bkt162RywRVqc7OYgg@mail.gmail.com> <4E64AC45.6030704@rem.nl> <20110906190548.433E8B5DBD1@mail.postgresql.org>

* 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