pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Kevin Burke <burke@shyp.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: UPDATE grabs multiple rows when it seems like it should only grab one
Date: Sat, 23 Apr 2016 11:31:37 -0400
Message-ID: <2997.1461425497@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwbmJcw0j=qjDjJaLA=97wB-J8fiz_e0-hObsN=s8J+nAw@mail.gmail.com>
References: <CAEYV4pYAG+o2_SO7+Tc9aUF8A9aZW=bNi2H7yd39i9fDy5nEEg@mail.gmail.com>
	<2934.1461366587@sss.pgh.pa.us>
	<CAKFQuwbmJcw0j=qjDjJaLA=97wB-J8fiz_e0-hObsN=s8J+nAw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> The understanding of JOIN that I hold is to take two complete relations
> and combine them on some predicate.  The from relation here, when complete,
> only has one row and given it is effectively a self-join on the PK the
> result of the join is guaranteed to be a single row.  I do not follow how
> the sub-select is allowed to be evaluated multiple times.

You're not perceiving it correctly.  In general, a join is defined as
"evaluate the join condition for each row pair in the cross-product
of the two relations, and take all the row pairs for which the join
condition succeeds".  The conditions in the query are effectively all
join conditions from a semantic standpoint; the fact that some are
syntactically written as a sub-select doesn't change that.  Kevin's query
will work as he expects only if the join condition is stable --- but here
it's volatile in two different ways, one being the underspecified LIMIT,
and the other one being the fact that FOR UPDATE allows the status
condition to see the effects of the outer UPDATE.

What's needed to make this work is to have a hard optimization boundary
between the sub-select and the outer query, so that these conditions don't
act like join conditions.  Merely writing sub-select syntax doesn't
provide that (and users wouldn't like it if it did).  I think that a CTE
should be a sufficient boundary, although even with a CTE, the FOR UPDATE
is a bit risky: if the outer update could change a row before the CTE's
scan had reached it, you'd still get funny results.  Should be OK in this
usage though, because you'd never update a row that the CTE hadn't already
examined.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (8+ messages)

Message-ID: <2997.1461425497@sss.pgh.pa.us>
Permalink:  ../2997.1461425497@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2997.1461425497@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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, burke@shyp.com
  Subject: Re: UPDATE grabs multiple rows when it seems like it should only grab one
  In-Reply-To: <2997.1461425497@sss.pgh.pa.us>
  References: <CAEYV4pYAG+o2_SO7+Tc9aUF8A9aZW=bNi2H7yd39i9fDy5nEEg@mail.gmail.com> <2934.1461366587@sss.pgh.pa.us> <CAKFQuwbmJcw0j=qjDjJaLA=97wB-J8fiz_e0-hObsN=s8J+nAw@mail.gmail.com> <2997.1461425497@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