Array exceed allowed size in Postgres
You can possibly limit the output of foo():

SELECT foo() LIMIT 10000;


Or, most probably better, avoid using arrays altogether (pass the set, not an array made of it).  For this, you will, of course, have to rewrite my_func().  Thinking about it, I can imagine that this will be beneficial from a performance point of view, too.