I wrote:
> In fact, it seems there's a different risk here: if such a datum were
> toasted out-of-line, the reference in a cached plan might live longer
> than the underlying toast-table data. Maybe we need a forcible detoast
> in evaluate_function().
Sure enough, it seems we do. The attached script fails in every release
back to 7.3. It's a rather contrived case, because a function marked
immutable probably shouldn't be reading from a table at all, especially
not one you are likely to change or drop. That's probably why we've not
heard reports of this before. But it's definitely a bug.
regards, tom lane
create table big(f1 text);
insert into big values(repeat('xyzzy',100000));
create or replace function getbig() returns text as
'select f1 from big' language sql immutable;
create or replace function usebig(text) returns bool as '
begin return $1 ~ ''xyzzy''; end
' language plpgsql;
prepare foo as select usebig(getbig()) from int4_tbl;
execute foo;
drop table big;
execute foo;