I am trying to create a stored function in PostgreSQL to improve performance and store large queries, and I just need to call the function after my code.
For example, if I have a function:
CREATE OR REPLACE FUNCTION test(max integer)
RETURNS TABLE (id integer) AS $$
SELECT User.id
FROM User
LIMIT max;
$$ LANGUAGE sql STABLE;
I call a function like this to see the duration of the request:
EXPLAIN ANALYZE SELECT test(10);
And the function is much slower than the same original SQL query! I thought that the saved function would be compiled and optimized upon creation. And if I try with a big request, the performance will be terrible with the function.
I think I'm probably something wrong!
Thank,
Ubord source
share