1

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.

2
  • You have a scalar function which returns a single value. Don't put that in the from clause, use select userexist(1); Commented Jun 24, 2015 at 15:39
  • Ok, otherwise performance are exactly the same with or without the from Commented Jun 24, 2015 at 15:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.