The function behaves as expected when in plain SQL, only plpgsqlfunction has the above mentioned problem.
regardsmk