Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jvTpr-0001J8-By for pgsql-general@arkaria.postgresql.org; Tue, 14 Jul 2020 22:55:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jvTpp-000485-2j for pgsql-general@arkaria.postgresql.org; Tue, 14 Jul 2020 22:55:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jvTpn-000410-FT for pgsql-general@lists.postgresql.org; Tue, 14 Jul 2020 22:55:36 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jvTpj-0005G5-1k for pgsql-general@lists.postgresql.org; Tue, 14 Jul 2020 22:55:35 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id ABFDD5C0114; Tue, 14 Jul 2020 18:55:27 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 14 Jul 2020 18:55:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=v thqgh0Uo0wTuRPposY/0E0Lpl97e+tqaWN1JKLLPsI=; b=Q5CBqhLC7ORUO5onB s4VEOSSXB3cxkOI2pZYE9Sz7WON/DfBOvUKfOlupszRwHgu/ZbNeb9N4jdjfxQ4L xActRKrCGML2N+uPf2bDPuUn1STAbAtqT2kCwoQ9AA+bujTdZvBlndcnFvOHyUZ2 CsXuC5s3/3E4dlCqBlXTUFfCsRsIcdcM9TNPh6oxsRnRAZPTWtcce+90wSwmOv6f cd4q6NAGrH9j1Ry7NRJ+BWV0LcMPXOdxmxraZNHBHOrf8scOp/tc2iFbfBTZ0NZ+ EwRxfm5DmfcqZTC0+5VpkEco6raja4KqVu+H7zV3BzpbPOkG1BEn0BzuOg1mHJXz dsRCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=vthqgh0Uo0wTuRPposY/0E0Lpl97e+tqaWN1JKLLP sI=; b=KQs1iD9c2awjWoMSwEP5mmGqXhBHExqBh+CHVY3I4BmzZz9QuII2462Eb 4HSUjlKo9jbNLctfYmJp67GKcW2Psr9GXgCUml9Mf2yfuulhfYsWrcKLlQ4fF8sl 6t8Tyoza6kT0Q8vkP3ePJHV+GU7xCsXL5vpoJJao+mX3tZ4aZcKMES1ED31HEOFR K6AM6yeUPjJlOmIDuGY3XVkhai+OjDr/yV33mmtnxQlv2YY3IhCO6gPsaHLAiYiH QVB62gF2hdLFftnzSSK8bNM3vRQwkPA9qglGWB3miuCucyVgAZsqgEwL2gmR1sAe yX/iL+hN46z0ievQMqeWwPNbZ/y/Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrfedugdduhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepkeehtdfgteekgfefleefveejhfettdfhudeutddtffdu teffvdejjedvffeitddunecuffhomhgrihhnpehmohhkrggusgdrtghomhenucfkphepje ehrddujedvrdehrddvfedunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id A8D42328005A; Tue, 14 Jul 2020 18:55:26 -0400 (EDT) Subject: Re: some random() clarification needed To: Marc Millas , "David G. Johnston" Cc: "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver Message-ID: <0311ced4-7a04-4d60-36ca-8bfbbef730f6@aklaver.com> Date: Tue, 14 Jul 2020 15:55:25 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/14/20 9:01 AM, Marc Millas wrote: > Hi, > your answer helps me understand my first problem. > so, I rewrote a simple loop so as to avoid the "volatile" behaviour. > (at least I was thinking I did... looks like I was wrong !) > step by step loop: > DO $$ > BEGIN >   FOR counter IN 1..1000 LOOP > begin > declare > id1 integer =ceiling(random()*2582); > id3 date= '2000-01-01'; > id2 date; > pren varchar; > begin > id2=id3 + (random()*7200)::integer; > SELECT prenom FROM prenoms WHERE id=id1 into pren; > INSERT INTO testparttransac (datenaissance, prenoms) values(id2,pren); >   end; > end; > END LOOP; > END; $$; > > I truncated the table, executed the loop with no errors, and expected > that a select count(*) > may answer 1000 ! > no. > it varies, from less than 1000 (much less, something like 900) > and more than 1000 (up to 1094) > > so... what s "volatile" in the loop ? I think it has more to do with the structure of the function. Not sure how all those nested BEGINs interact, but when I simplify the above to: DO $$ DECLARE id1 integer =ceiling(random()*2582); id3 date= '2000-01-01'; id2 date; pren varchar = 'test'; BEGIN FOR counter IN 1..1000 LOOP id2 = id3 + (random()*7200)::integer; INSERT INTO testparttransac (datenaissance, prenoms) values(id2,pren); END LOOP; END; $$; I get 1000 rows each time I TRUNCATE testparttransac and then run above and repeat. > > BTW the testparttransac table is partitioned on datenaissance, with a > default partition. > > thanks, > regards, > > > Marc MILLAS > Senior Architect > +33607850334 > www.mokadb.com > > > > On Tue, Jul 14, 2020 at 5:24 PM David G. Johnston > > wrote: > > On Tue, Jul 14, 2020 at 8:15 AM Marc Millas > wrote: > > select id, prenom from prenoms where id=ceiling(random()*2582); > > expecting to get, allways, one line. > But its not the case. > around 15% of time I get 0 lines which is already quite strange > to me. > but 10% of time, I get a random number of lines, until now up to 4. > even weirder (to me !) > > so, can someone please clarify ? > > > You are basically asking: > > For each row in my table compare the id to some random number and if > they match return that row, otherwise skip it.  The random number > being compared to is different for each row because random() is > volatile and thus evaluated for each row. > > David J. > -- Adrian Klaver adrian.klaver@aklaver.com