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 1elKRr-0004ML-Tx for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Feb 2018 20:11:36 +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 1elKRq-0000Mq-OG for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Feb 2018 20:11:34 +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 1elKRq-0000Mh-Ir for pgsql-bugs@lists.postgresql.org; Mon, 12 Feb 2018 20:11:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1elKRn-0007X8-QM for pgsql-bugs@lists.postgresql.org; Mon, 12 Feb 2018 20:11:34 +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 w1CKBRDH012103; Mon, 12 Feb 2018 15:11:27 -0500 From: Tom Lane To: Andrew Gierth cc: 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: <876072rphi.fsf@news-spur.riddles.org.uk> References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> <876072rphi.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Mon, 12 Feb 2018 19:31:00 +0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <12044.1518466218.0@sss.pgh.pa.us> Date: Mon, 12 Feb 2018 15:11:27 -0500 Message-ID: <12102.1518466287@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <12044.1518466218.1@sss.pgh.pa.us> Andrew Gierth writes: > 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). Yeah, I came to the same conclusion. I think it's basically accidental that the given test case works before 9.2: the reason seems to be that in 9.1, the plancache doesn't pass through the parameter list containing the value of "my_text", so that the planner is unable to speculatively execute get_bug_id(). The order of operations in _SPI_execute_plan is just as wrong though. The attached patch makes it better and doesn't seem to break any regression tests. I'm not sure how nervous I am about unexpected side-effects ... regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="set-snapshot-sooner-in-spi.patch"; charset="us-ascii" Content-ID: <12044.1518466218.2@sss.pgh.pa.us> Content-Description: set-snapshot-sooner-in-spi.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c index 9fc4431..45e62ba 100644 *** a/src/backend/executor/spi.c --- b/src/backend/executor/spi.c *************** _SPI_execute_plan(SPIPlanPtr plan, Param *** 2066,2071 **** --- 2066,2083 ---- spierrcontext.arg =3D (void *) plansource->query_string; = /* + * In the default non-read-only case, get a new snapshot, replacing + * any that we pushed in a previous cycle. + */ + if (snapshot =3D=3D InvalidSnapshot && !read_only) + { + if (pushed_active_snap) + PopActiveSnapshot(); + PushActiveSnapshot(GetTransactionSnapshot()); + pushed_active_snap =3D true; + } + = + /* * If this is a one-shot plan, we still need to do parse analysis. */ if (plan->oneshot) *************** _SPI_execute_plan(SPIPlanPtr plan, Param *** 2117,2134 **** cplan =3D GetCachedPlan(plansource, paramLI, plan->saved, _SPI_current= ->queryEnv); stmt_list =3D cplan->stmt_list; = - /* - * In the default non-read-only case, get a new snapshot, replacing - * any that we pushed in a previous cycle. - */ - if (snapshot =3D=3D InvalidSnapshot && !read_only) - { - if (pushed_active_snap) - PopActiveSnapshot(); - PushActiveSnapshot(GetTransactionSnapshot()); - pushed_active_snap =3D true; - } - = foreach(lc2, stmt_list) { PlannedStmt *stmt =3D lfirst_node(PlannedStmt, lc2); --- 2129,2134 ---- ------- =_aaaaaaaaaa0--