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:07:20 +0800
Message-ID: <20110908140745.8B0CCB5DBE0@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:
>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.

Yeah it works if all the inserters do the lock table (or select for 
update), and provides better performance.

But if you're paranoid and lazy - a full lock will ensure that your 
code won't get dupe errors even if someone else's code or manual 
control doesn't do the lock table (they might get the dupe errors[1], 
but that's their problem ;) ). So your code can safely assume that 
any DB errors that occur are those that deserve a full rollback of 
everything (which is what Postgresql "likes" by default). This means 
fewer scenarios to handle so you don't need to write as much code, 
nor document and support as much code ;).

Link.

[1] I'm assuming a unique constraint is present- the locking is to 
simplify things.




view thread (36+ messages)  latest in thread

Message-ID: <20110908140745.8B0CCB5DBE0@mail.postgresql.org>
Permalink:  ../20110908140745.8B0CCB5DBE0@mail.postgresql.org/
Also on:    postgresql.org/message-id/20110908140745.8B0CCB5DBE0@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: <20110908140745.8B0CCB5DBE0@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> <20110908140745.8B0CCB5DBE0@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