From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: pgsql-bugs@lists.postgresql.org
Cc: dejan.petrovic@islonline.com
Subject: Re: BUG #15060: Row in table not found when using pg function in an expression
Date: Mon, 12 Feb 2018 15:11:27 -0500
Message-ID: <12102.1518466287@sss.pgh.pa.us> (raw)
In-Reply-To: <876072rphi.fsf@news-spur.riddles.org.uk>
References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org>
<876072rphi.fsf@news-spur.riddles.org.uk>
Andrew Gierth <andrew@tao11.riddles.org.uk> 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
Attachments:set-snapshot-sooner-in-spi.patch (text/x-diff, 1.4K) downloaddiff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.cindex 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 = (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 == InvalidSnapshot && !read_only)+ {+ if (pushed_active_snap)+ PopActiveSnapshot();+ PushActiveSnapshot(GetTransactionSnapshot());+ pushed_active_snap = 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 = GetCachedPlan(plansource, paramLI, plan->saved, _SPI_current->queryEnv);
stmt_list = 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 == InvalidSnapshot && !read_only)- {- if (pushed_active_snap)- PopActiveSnapshot();- PushActiveSnapshot(GetTransactionSnapshot());- pushed_active_snap = true;- }-
foreach(lc2, stmt_list)
{
PlannedStmt *stmt = lfirst_node(PlannedStmt, lc2);
--- 2129,2134 ----
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, andrew@tao11.riddles.org.uk, dejan.petrovic@islonline.com
Subject: Re: BUG #15060: Row in table not found when using pg function in an expression
In-Reply-To: <12102.1518466287@sss.pgh.pa.us>
References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> <876072rphi.fsf@news-spur.riddles.org.uk> <12102.1518466287@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox