pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jeff Janes <jeff.janes@gmail.com>
Cc: Durumdara <durumdara@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Select for update / deadlock possibility?
Date: Tue, 02 Jan 2018 10:23:00 -0500
Message-ID: <32466.1514906580@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMkU=1yMtsFnmh4uVYUf21mRCcikCOwdfm9M6rJAeBJ_T9eHcQ@mail.gmail.com>
References: <CAEcMXhnS+h8Luy7eosgkVHJgXZorjGQZ+9OP6TZrzkZWXCFgxg@mail.gmail.com>
	<CAMkU=1yMtsFnmh4uVYUf21mRCcikCOwdfm9M6rJAeBJ_T9eHcQ@mail.gmail.com>

Jeff Janes <jeff.janes@gmail.com> writes:
> On Tue, Jan 2, 2018 at 3:22 AM, Durumdara <durumdara@gmail.com> wrote:
>> Is "select for update" atomic (as transactions) or it isn't?

> It is atomic, but you do have to worry about deadlocks.

I think by "atomic" the OP intends "all the row locks are magically
acquired at the same instant".  Which they aren't, they're taken one
at a time.  So if you have different transactions trying to lock
overlapping sets of rows, there's a risk of deadlock.  Which will
be detected and one transaction will fail, but still you might wish
to avoid that.

The usual rule for that is "be sure all transactions acquire locks
in the same order".  So just throw an "order by id" type of clause
into the SELECT FOR UPDATE, and you should be fine.

Personally, I'd still code the application to retry on deadlock
failures, just for robustness.

			regards, tom lane




view thread (6+ messages)

Message-ID: <32466.1514906580@sss.pgh.pa.us>
Permalink:  ../32466.1514906580@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/32466.1514906580@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, jeff.janes@gmail.com, durumdara@gmail.com
  Subject: Re: Select for update / deadlock possibility?
  In-Reply-To: <32466.1514906580@sss.pgh.pa.us>
  References: <CAEcMXhnS+h8Luy7eosgkVHJgXZorjGQZ+9OP6TZrzkZWXCFgxg@mail.gmail.com> <CAMkU=1yMtsFnmh4uVYUf21mRCcikCOwdfm9M6rJAeBJ_T9eHcQ@mail.gmail.com> <32466.1514906580@sss.pgh.pa.us>

* 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