pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Lars Vonk <lars.vonk@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15715: UPDATE using "in (subquery for update limit 1)" does not respect the limit in subquery
Date: Tue, 26 Mar 2019 18:53:56 -0400
Message-ID: <16497.1553640836@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMX1Thhr8X5e=t6n+m=kWR1LhOqoqe-wupyBZKuuxmpr3091FA@mail.gmail.com>
References: <15715-1eccf9c7dffc4093@postgresql.org>
	<1134.1553619797@sss.pgh.pa.us>
	<CAMX1Thhr8X5e=t6n+m=kWR1LhOqoqe-wupyBZKuuxmpr3091FA@mail.gmail.com>

Lars Vonk <lars.vonk@gmail.com> writes:
> Thanks for the explanation. Our hotfix was to replace the IN with a =  so
> the query fails and the transaction is rolled back when more than one is
> returned from the sub-SELECT.

And have you actually seen any such failures?  If you have, then there
is certainly something going wrong here.  The LIMIT should never allow
more than one row to be returned by the sub-SELECT, period, full stop.

The issue here is that the sub-SELECT is, in principle, executed afresh
for every row of the outer query, and you do not have a guarantee that
each such execution returns the *same* single row.  Ordinary query
execution would provide such a guarantee, but you're using FOR UPDATE
on a table that's being modified concurrently (including by this query
itself), and therefore the guarantee disappears.

You might think that since the sub-SELECT is uncorrelated with the outer
query, there's no need to execute it more than once ... but that's an
optimization, not part of the guaranteed semantics.  Without seeing an
EXPLAIN for this query on your system, we can't know whether it's being
done like that (though the fact that you're complaining suggests that
it isn't).

Interestingly, it's quite likely that your "hotfix" made the problem
go away, because PG generally *does* optimize uncorrelated sub-SELECTs
to be executed only once if they return scalar results.  That doesn't
happen for IN subqueries though, as those are treated as joins.

Either way, a WITH is probably preferable because that does get you
into a place where we guarantee single-evaluation semantics.

			regards, tom lane





view thread (7+ messages)  latest in thread

Message-ID: <16497.1553640836@sss.pgh.pa.us>
Permalink:  ../16497.1553640836@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16497.1553640836@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, lars.vonk@gmail.com
  Subject: Re: BUG #15715: UPDATE using "in (subquery for update limit 1)" does not respect the limit in subquery
  In-Reply-To: <16497.1553640836@sss.pgh.pa.us>
  References: <15715-1eccf9c7dffc4093@postgresql.org> <1134.1553619797@sss.pgh.pa.us> <CAMX1Thhr8X5e=t6n+m=kWR1LhOqoqe-wupyBZKuuxmpr3091FA@mail.gmail.com> <16497.1553640836@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