Postgresql Function Select Count Sql Query To Find