From: Tom Lane <tgl@sss.pgh.pa.us>
To: frank.von.postgresql.org@familie-gard.de, Frank Gard <frank@familie-gard.de>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #14737: Wrong PL/pgSQL behaviour
Date: Fri, 07 Jul 2017 17:39:04 -0400
Message-ID: <7460.1499463544@sss.pgh.pa.us> (raw)
In-Reply-To: <19713a56-5ac0-4749-de0e-0a3ebb546b73@familie-gard.de>
References: <20170707161041.9034.61885@wrigleys.postgresql.org>
<CAKFQuwZn6DOm=+8vL3AnTXM4Jt1jJZFADpHOmk6WwfpXiZ5srA@mail.gmail.com>
<19713a56-5ac0-4749-de0e-0a3ebb546b73@familie-gard.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Frank Gard <frank@familie-gard.de> writes:
> The problem here is not that RANDOM() has different values for each row.
> This is exactly the expected behaviour. But if I do a numerical FOR loop
> iterating the range from 1 to p_anzahl, and every time I do an UPDATE
> which increments a "counter", then after the loop, the counter should
> equal to p_anzahl (and nothing else).
The flaw in that argument is the assumption that each execution of UPDATE
updates exactly one row. But since RANDOM() is recomputed at each row,
what you really have is a stochastic decision whether to update that row,
and so the UPDATE could well update more or fewer than one row overall.
> In addition, the result should not differ at all between the two variants of
> the function. What makes, semantically, the difference? There's none at all!
The point is exactly that a WHERE condition is (notionally) computed at
each table row, and that includes re-evaluating any functions it calls.
The DBMS can often optimize away some of that computation, but in this
case it cannot because of the volatility of RANDOM().
The SQL standard is pretty explicit that this is the intended semantics
--- it says "SC is effectively evaluated for each row of T". (In the 2011
edition, this is in 14.14 <update statement: searched>, general rule 5.)
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
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, frank@familie-gard.de, david.g.johnston@gmail.com
Subject: Re: BUG #14737: Wrong PL/pgSQL behaviour
In-Reply-To: <7460.1499463544@sss.pgh.pa.us>
References: <20170707161041.9034.61885@wrigleys.postgresql.org> <CAKFQuwZn6DOm=+8vL3AnTXM4Jt1jJZFADpHOmk6WwfpXiZ5srA@mail.gmail.com> <19713a56-5ac0-4749-de0e-0a3ebb546b73@familie-gard.de> <7460.1499463544@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