pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: pgsql-bugs@lists.postgresql.org
To: 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 19:31:00 +0000
Message-ID: <876072rphi.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org>
References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org>

>>>>> "PG" == PG Bug reporting form <noreply@postgresql.org> 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)




view thread (11+ messages)  latest in thread

Message-ID: <876072rphi.fsf@news-spur.riddles.org.uk>
Permalink:  ../876072rphi.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/876072rphi.fsf@news-spur.riddles.org.uk

 · 

reply

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: 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: <876072rphi.fsf@news-spur.riddles.org.uk>
  References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> <876072rphi.fsf@news-spur.riddles.org.uk>

* 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