-werners-
Esteemed Contributor III

Probably some bug in your function.

What I suggest is to first execute all the joins manually and run an explain to get the query plan.

Than compare that query plan to the one created by your function.

Especially if you do a loop in your function, it will probably be the culprit.