Hi all,
I have this strange behavior when I use temp table with same name of a permanent table in a function.
IMO, you are asking for strange behavior when you overload a table name.
I expect that one of the people who work down deep down in the code will say it is a caching thing that saves system table lookups. It also may be required to ensure consistent behavior when you use the same table name (non specified schema) then you should always get the same table.
Still, it might be simple to use exceptions and try to read the pg_temp table and if it does not exist, try the public schema.
Or, just schema qualify the fetch call with a string that is computed for which schema you want to use.