pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lincoln Yeoh <lyeoh@pop.jaring.my>
To: Merlin Moncure <mmoncure@gmail.com>
Cc: jhondius@rem.nl
Cc: Pau Marc Muñoz Torres <paumarc@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: conditional insert
Date: Thu, 08 Sep 2011 22:14:02 +0800
Message-ID: <20110908141416.9BBA9B5DBCB@mail.postgresql.org> (raw)
In-Reply-To: <CAHyXU0w__hAwXRujM_JxF-4tg_hRYc4JCY_-_3DiyjzE2aYR+w@mail.g mail.com>
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>
	<CAHyXU0zSE4aVoZhXL9i8hnSjRaJwOpmcrV+jfLKKY=OCdXhR6A@mail.gmail.com>
	<CAHyXU0x3jMadwoZFW8S5+g04VjBGURNUnEYN4ZzhmAy5CQaTGw@mail.gmail.com>
	<4e679195.12848e0a.019e.ffff918eSMTPIN_ADDED@mx.google.com>
	<CAHyXU0w__hAwXRujM_JxF-4tg_hRYc4JCY_-_3DiyjzE2aYR+w@mail.gmail.com>

At 03:51 AM 9/8/2011, Merlin Moncure wrote:
> > Don't you have to block SELECTs so that the SELECTs get serialized?
> > Otherwise concurrent SELECTs can occur at the same time, find no existing
> > rows, then "all" the inserts proceed and you get errors (or dupes).
> >
> > That's how Postgresql still works right? I haven't really been keeping up.
>
>yeah -- but you only need to block selects if you are selecting in the
>inserting transaction (this is not a full upsert).  if both writers
>are doing:
>begin;
>lock table foo exclusive;
>insert into foo select ... where ...;
>commit;
>
>is good enough.  btw even if you are doing upsert pattern
>(lock...select for update...insert/update), you'd be fine with
>straight exclusive locks because the 'for update' lock takes a higher
>lock that is blocked by exclusive.  A basic rule of thumb is to try
>and not fully block readers unless absolutely necessary...basically
>maintenance operations.

Oh wait, now I think I get it. "lock table foo exclusive" will block 
the inserts too, so I wouldn't get dupe errors even if other 
transactions "blindly" insert dupes at the same time. The other 
transactions might get the dupe errors, but mine won't as long as it 
selects first and only inserts if there are no rows at that point.

Is that correct?

Link.




view thread (36+ messages)  latest in thread

Message-ID: <20110908141416.9BBA9B5DBCB@mail.postgresql.org>
Permalink:  ../20110908141416.9BBA9B5DBCB@mail.postgresql.org/
Also on:    postgresql.org/message-id/20110908141416.9BBA9B5DBCB@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, mmoncure@gmail.com, jhondius@rem.nl, paumarc@gmail.com
  Subject: Re: conditional insert
  In-Reply-To: <20110908141416.9BBA9B5DBCB@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> <CAHyXU0zSE4aVoZhXL9i8hnSjRaJwOpmcrV+jfLKKY=OCdXhR6A@mail.gmail.com> <CAHyXU0x3jMadwoZFW8S5+g04VjBGURNUnEYN4ZzhmAy5CQaTGw@mail.gmail.com> <4e679195.12848e0a.019e.ffff918eSMTPIN_ADDED@mx.google.com> <CAHyXU0w__hAwXRujM_JxF-4tg_hRYc4JCY_-_3DiyjzE2aYR+w@mail.gmail.com> <20110908141416.9BBA9B5DBCB@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