Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1elJom-00010u-T9 for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Feb 2018 19:31:12 +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 1elJom-0006yr-1f for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Feb 2018 19:31:12 +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 1elJol-0006yZ-P3 for pgsql-bugs@lists.postgresql.org; Mon, 12 Feb 2018 19:31:11 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1elJoi-0005fC-I2 for pgsql-bugs@lists.postgresql.org; Mon, 12 Feb 2018 19:31:10 +0000 Received: from [192.168.127.1] (port=38894 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1elJod-000NXZ-Up; Mon, 12 Feb 2018 19:31:03 +0000 Received: from [127.0.0.1] (port=31550 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1elJob-000FgF-QV; Mon, 12 Feb 2018 19:31:01 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, dejan.petrovic@islonline.com Subject: Re: BUG #15060: Row in table not found when using pg function in an expression In-Reply-To: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> (PG Bug reporting form's message of "Mon, 12 Feb 2018 12:59:04 +0000") Message-ID: <876072rphi.fsf@news-spur.riddles.org.uk> References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Mon, 12 Feb 2018 19:31:00 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form writes: PG> I tested this in postgresql versions 9.1, 10.1 and 10.2 on centOS. PG> In short this is what happens (in a plpgsql function): PG> 1.) An insert is done into 'bug' table PG> 2.) A SELECT is done to make sure the INSERT was successful PG> 3.) Another function (get_bug_id) is called which returns id based on PG> value. PG> When the function is called directly, it returns the id correctly. PG> When it's called in an expression, it does not find the inserted PG> row and an exception is raised. So what's happening here is that the function get_bug_id, being stable, is being called speculatively at plan time for the query where it appears in the WHERE clause. For whatever reason, the snapshot it's being run in at that time is not the same one actually used for the later execution of the query, and the plan-time snapshot doesn't see the just-inserted row. It looks like what's going on here is that SPI does GetCachedPlan - which is where planning will happen - _before_ establishing the new snapshot in the non-read-only case (read_only is false here because the calling function, test_bug(), is volatile). -- Andrew (irc:RhodiumToad)