Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1edvcP-0001kE-S4 for pgsql-general@arkaria.postgresql.org; Tue, 23 Jan 2018 10:15:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1edvcP-0000T2-4p for pgsql-general@arkaria.postgresql.org; Tue, 23 Jan 2018 10:15:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1edvcO-0000Sr-Re for pgsql-general@lists.postgresql.org; Tue, 23 Jan 2018 10:15:52 +0000 Received: from forward3p.cmail.yandex.net ([2a02:6b8:0:1465::13]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1edvcH-0004sC-V6 for pgsql-general@lists.postgresql.org; Tue, 23 Jan 2018 10:15:52 +0000 Received: from mxback5j.mail.yandex.net (mxback5j.mail.yandex.net [IPv6:2a02:6b8:0:1619::10e]) by forward3p.cmail.yandex.net (Yandex) with ESMTP id E36C820ED1; Tue, 23 Jan 2018 13:15:42 +0300 (MSK) Received: from web17j.yandex.ru (web17j.yandex.ru [2a02:6b8:0:1619::317]) by mxback5j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id OjyLKcBErH-FfoiEHW3; Tue, 23 Jan 2018 13:15:42 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ya.ru; s=mail; t=1516702542; bh=nSvEcGDW5D43WZ4vz4aqgrlsX7FyK2BdxsxuHuJ3lPA=; h=From:To:Cc:References:Subject:Message-Id:Date; b=o4GHM6/zGPUbMXEva7DrZ6JEjkfGi2B94X+1EOL/3QpqGzx5JaXVkehmej5BHrJHm CVP067DDtKLhvB6VfsUgEwdOVt8IoT2Vj4+Zfb8hpfTDnKgAJvjJjQDbuhM8J1T0pO GgN25Hy2+v+arCAR9aZLvmQU1irOoMXJjKeGutAA= Authentication-Results: mxback5j.mail.yandex.net; dkim=pass header.i=@ya.ru Received: by web17j.yandex.ru with HTTP; Tue, 23 Jan 2018 13:15:41 +0300 From: Olleg Samoylov Envelope-From: splarv@yandex.ru To: Tom Lane , Jeff Janes Cc: "pgsql-general@lists.postgresql.org" References: <471561516635427@web43o.yandex.ru> <15531.1516641381@sss.pgh.pa.us> <9738.1516652155@sss.pgh.pa.us> Subject: Re: Using random() in update produces same random value for all MIME-Version: 1.0 Message-Id: <3643511516702541@web17j.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Tue, 23 Jan 2018 13:15:41 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2018-01-22 23:15, Tom Lane wrote: > > It is honored as volatile: it will be re-evaluated every time the > sub-select is re-evaluated. It's just that there's no cause to > re-evaluate the sub-select. > > I poked through the SQL standard to see if it spells out the semantics > of uncorrelated subqueries anywhere, and couldn't find anything relevant > at all. But this is how Postgres has understood the behavior of > sub-selects for a very long time (~20 years). I'm pretty certain > that there are people depending on it to behave this way. > > regards, tom lane The cause exists, the function is volatile and according to definition it must be recalculated every time. But well, one more example. => select generate_series,(select random+generate_series from random()) from generate_series(1,10);  generate_series |     ?column? -----------------+------------------                1 | 1.94367738347501                2 | 2.94367738347501                3 | 3.94367738347501                4 | 4.94367738347501                5 | 5.94367738347501                6 | 6.94367738347501                7 | 7.94367738347501                8 | 8.94367738347501                9 | 9.94367738347501               10 |  10.943677383475 (10 rows) As you can see, sub-select is indeed recalculated, but not random(). And this is may be right, because random() is used as source off data. Another example. => select generate_series,(select random()+generate_series) from generate_series(1,10);  generate_series |     ?column? -----------------+------------------                1 | 1.37678202055395                2 |  2.5316761219874                3 | 3.33511888468638                4 |  4.0293406387791                5 | 5.69305071979761                6 | 6.33374964864925                7 | 7.14478175388649                8 |  8.1831739502959                9 |  9.4472619513981               10 | 10.2977624684572 (10 rows) Here random() is recalculated as sub-select. But in => select *,(select random()) from generate_series(1,10);  generate_series |      random -----------------+-------------------                1 | 0.487761380150914                2 | 0.487761380150914                3 | 0.487761380150914                4 | 0.487761380150914                5 | 0.487761380150914                6 | 0.487761380150914                7 | 0.487761380150914                8 | 0.487761380150914                9 | 0.487761380150914               10 | 0.487761380150914 (10 rows) is not. IMHO all this behavior may be not bad, but it must be well documented in manual in section about sub-selects. All sub-select must be documented as "stable" in terms of function definition. And thus will not be surprise.