I recently implemented something closely related to this. Combining and
migrating expensive STABLE user-defined functions to the FROM clause, where
the function is evaluated as a lateral join (or "cross apply"). I'm
defining expensive as 50x times more expensive than the default function
cost.
For functions that return multiple outputs and where the query uses (...).*
notation, this will, for example, consolidate all of the calls in the *
expansion into a single call. It also looks in the WHERE clause and HAVING
clause, and combines those references, too. Currently it requires the
function to be in a top-level AND condition, if it appears in a predicate.
I think I can get permission for contributing it back. If there's an
interest in it, let me know.
-----
Jim Finnerty, AWS, Amazon Aurora PostgreSQL
--
Sent from: https://www.postgresql-archive.org/PostgreSQL-hackers-f1928748.html