Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dTay2-00015Q-41 for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Jul 2017 21:39:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dTay1-0005Zd-89 for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Jul 2017 21:39:13 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dTay0-0005ZL-Ka for pgsql-bugs@postgresql.org; Fri, 07 Jul 2017 21:39:12 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dTaxy-0002YF-34 for pgsql-bugs@postgresql.org; Fri, 07 Jul 2017 21:39:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v67Ld4Vc007461; Fri, 7 Jul 2017 17:39:04 -0400 From: Tom Lane To: frank.von.postgresql.org@familie-gard.de, Frank Gard cc: "David G. Johnston" , pgsql-bugs@postgresql.org Subject: Re: BUG #14737: Wrong PL/pgSQL behaviour In-reply-to: <19713a56-5ac0-4749-de0e-0a3ebb546b73@familie-gard.de> References: <20170707161041.9034.61885@wrigleys.postgresql.org> <19713a56-5ac0-4749-de0e-0a3ebb546b73@familie-gard.de> Comments: In-reply-to Frank Gard message dated "Fri, 07 Jul 2017 22:49:02 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <7459.1499463544.1@sss.pgh.pa.us> Date: Fri, 07 Jul 2017 17:39:04 -0400 Message-ID: <7460.1499463544@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Frank Gard 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 , 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