pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Marko Tiikkaja <marko@joh.to>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
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: Tue, 13 Feb 2018 12:35:25 +0000
Message-ID: <87vaf1qfps.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAL9smLDF9pZ89LWFgO4BcnMZf15EFiaxAhcnjuamS=HOKb+=NQ@mail.gmail.com>
References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org>
	<876072rphi.fsf@news-spur.riddles.org.uk>
	<12102.1518466287@sss.pgh.pa.us>
	<CAL9smLDF9pZ89LWFgO4BcnMZf15EFiaxAhcnjuamS=HOKb+=NQ@mail.gmail.com>

>>>>> "Marko" == Marko Tiikkaja <marko@joh.to> writes:

 >>> 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.

 Marko> I'm not sure I understand. When's the snapshot used for planning
 Marko> actually taken here?

GetCachedPlan will use either whatever snapshot is already set, if there
is one, or it will set one of its own (actually at least two: separate
snapshots for revalidate + parse analysis and for planning).

In the case of a volatile plpgsql function, the snapshot in which the
function was called will, I believe, still be the active snapshot at the
relevant point, so calls made in planning won't see the function's own
changes.

The recent introduction of procedures exposes this interesting little
variation in behavior (pg11 only):

create table bug (id integer, d text);
create or replace function getbug(text) returns integer
  language plpgsql stable
  as $$
    declare
      b_id integer;
    begin
      select into b_id id from bug where d = $1;
      if not found then
        raise info 'bug % not found',$1;
      else
        raise info 'bug % id %',$1,b_id;
      end if;
      return b_id;
    end;
$$;

truncate table bug;
do $$
  begin
    insert into bug values (1,'foo');
    perform * from bug where id = getbug('foo');
  end;
$$;
INFO:  bug foo not found
INFO:  bug foo id 1

truncate table bug;
do $$
  begin
    commit;
    insert into bug values (1,'foo');
    perform * from bug where id = getbug('foo');
  end;
$$;
INFO:  bug foo id 1
INFO:  bug foo id 1

I assume that what's going on here is that the commit, which ends the
transaction in which the DO was invoked and begins a new one, doesn't
set a new active snapshot in the new transaction, and so planning of the
perform in the second case is taking new snapshots inside GetCachedPlan.

-- 
Andrew (irc:RhodiumToad)




view thread (11+ messages)  latest in thread

Message-ID: <87vaf1qfps.fsf@news-spur.riddles.org.uk>
Permalink:  ../87vaf1qfps.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87vaf1qfps.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, marko@joh.to, tgl@sss.pgh.pa.us, dejan.petrovic@islonline.com
  Subject: Re: BUG #15060: Row in table not found when using pg function in an expression
  In-Reply-To: <87vaf1qfps.fsf@news-spur.riddles.org.uk>
  References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> <876072rphi.fsf@news-spur.riddles.org.uk> <12102.1518466287@sss.pgh.pa.us> <CAL9smLDF9pZ89LWFgO4BcnMZf15EFiaxAhcnjuamS=HOKb+=NQ@mail.gmail.com> <87vaf1qfps.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