Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgCfQ-0005En-Pf for pgsql-general@arkaria.postgresql.org; Thu, 19 Jul 2018 17:24:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fgCfO-0000fD-Ii for pgsql-general@arkaria.postgresql.org; Thu, 19 Jul 2018 17:24:38 +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.89) (envelope-from ) id 1fgCfO-0000f5-Aj for pgsql-general@lists.postgresql.org; Thu, 19 Jul 2018 17:24:38 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgCfL-00048i-2C for pgsql-general@postgresql.org; Thu, 19 Jul 2018 17:24:37 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id E72215E3; Thu, 19 Jul 2018 13:24:32 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 19 Jul 2018 13:24:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=OCr1tkWCwECVehsShlnFVo08Pfi1f jWCztJHF2xAC3w=; b=selBcs5TaV18bTK+xThQ5Kq3SLstOEgKpmNbMqMZ0RjX6 YdgEqd70JaHnX8Hye2eMeO9Jj2b3H+mMYB9LAoJvvmNQngbf90Ws2zvvdAnTYQc4 VSFPE4ihCuh18g2I7fMTA+2JOoH89c2czuKupzF6BL3GltMtRUhlh1UwIB2BP9GK 7Bl9hSkaRjGrmSZkb6ROeUAR7Tr7+TNvjjJjpO8d5yd7rj+EAZNneGE4XbwCccud qmu2ckFlzfQmrNK/iX0LP5LEw4+rQlJtFYNF2UpqoxXJ7j7WVb4MRydGBVFUBtAv GvZW9NPyL6oriK89RrBm+NveX0duCtMn5lTsjlwJg== 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-sender:x-me-sender:x-sasl-enc; s=fm3; bh=OCr1tk WCwECVehsShlnFVo08Pfi1fjWCztJHF2xAC3w=; b=h8TsMs0kN8t0bKQkUrDUUq PCoMKTuL1MrXVkI5VJ7F+hrMScw3sy7jRUQ9hDqXxahEwAzle8yt4RqZy7jELeBF xPNMIPqLro+RcEJyG7KbDiomE/bSVjOMhPPM3R/1wFTH+RUDRNvWV/0MxtnpPExc DMuV5m77J2JSCJfeqqsDgaG7QdZytQK8ChZwKBu8LaTm6etpcT2EqRrAs3UCk/Ax LeNNI2MtFJDWuytCXP5ZYncRILmfGClHBHqyZ/XhNVAmFuvffm6Nm9wOTcFWJJ23 wvn9wVS9vG9bxvgvX7PLRfKcov9caynGy3jaZM4LUSTOsFBhmhU+71GH7BEcEpaw == X-ME-Proxy: X-ME-Sender: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id C2792E4511; Thu, 19 Jul 2018 13:24:31 -0400 (EDT) Subject: Re: functions with side effect To: =?UTF-8?Q?Torsten_F=c3=b6rtsch?= Cc: PostgreSQL mailing lists References: From: Adrian Klaver Message-ID: <0bf26f27-7b67-c344-35fd-d4be6f8d21b2@aklaver.com> Date: Thu, 19 Jul 2018 10:24:31 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 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 07/19/2018 09:43 AM, Torsten Förtsch wrote: > On Thu, Jul 19, 2018 at 6:35 PM Adrian Klaver > wrote: > > On 07/19/2018 07:15 AM, Torsten Förtsch wrote: > > Hi, > > > > assuming > > > > SELECT nextval('s'), currval('s'); > > > > or > > > > SELECT * FROM (VALUES (nextval('s'), currval('s'))) t; > > > > is there any guarantee that the 2 output values are the same? > > Assuming you are only working in single session: > > https://www.postgresql.org/docs/10/static/functions-sequence.html > > "currval > >      Return the value most recently obtained by nextval for this > sequence in the current session. (An error is reported if nextval has > never been called for this sequence in this session.) Because this is > returning a session-local value, it gives a predictable answer whether > or not other sessions have executed nextval since the current > session did." > > > I know that. My question was about the execution order of f1 and f2 in > "SELECT f1(), f2()". In theory they can be executed in any order. But > since the side effect in nextval determines the result of currval, I am > asking if that order is well-defined or considered an implementation > detail like in C. > To eliminate plan caching: DO $$ DECLARE rs record; BEGIN FOR i IN 1..1000 LOOP EXECUTE 'SELECT nextval($1), currval($1)' INTO rs USING 'order_test'; RAISE NOTICE 'Currval is %', rs.currval; END LOOP; END$$; It still works over multiple runs, even when bumping LOOP counter to 100,000. -- Adrian Klaver adrian.klaver@aklaver.com