Some people on the #postgresql irc channel pointed out that it's a known
issue.
http://www.qaix.com/postgresql-database-development/246-557-select-based-on-function-result-read.shtml
A more simple testcase is below. Adding OFFSET 0 to the inner query does
indeed fix it in my case.
SELECT
tmp.
* Tom Lane <[EMAIL PROTECTED]> wrote:
> Enrico Weigelt <[EMAIL PROTECTED]> writes:
> > c) CREATE FUNCTION id2username(oid) RETURNS text
> > LANGUAGE 'SQL' IMMUTABLE AS '
> > SELECT username AS RESULT FROM users WHERE uid = $1';
>
> This is simply dangerous. The function is *NOT* immutabl