I'm trying to make a function in PostgreSQL which returns true or false if a row exist. It's very easy in SQL, and performance is good:
SELECT exists (SELECT 1
FROM Users u
WHERE u.id = $1)
But when I try to create a function with this code, the performance is terrible:
CREATE OR REPLACE FUNCTION userExist(userId integer)
RETURNS boolean AS $$
SELECT exists (SELECT 1
FROM User u
WHERE u.id = $1);
$$ LANGUAGE sql STABLE;
I call this function like that :
EXPLAIN ANALYZE SELECT * FROM userExist(1);
Seems that the problem is the same as a previous question I ask ( PostgreSQL Performance - SELECT vs Stored function ), but the answer i get last time doesn't work in this situation.
fromclause, useselect userexist(1);from