create function is_movie_finished(movie_tmdb_id integer, user_email text) returns boolean language plpgsql as $$ DECLARE is_finished BOOLEAN:= false; user_data text; BEGIN SELECT tmdb_id INTO user_data FROM rpiwebapp.user_tv_movie_data WHERE tmdb_id=movie_tmdb_id AND "user"=user_email AND finished=true; IF user_data IS NOT NULL THEN is_finished:= true; END IF; RETURN is_finished; END $$; create function is_tv_show_finished(show_id integer, user_email text) returns boolean language plpgsql as $$ DECLARE len_user_data INTEGER; len_tv_episodes INTEGER; is_finished BOOLEAN:= false; BEGIN SELECT count(*) INTO len_user_data FROM rpiwebapp.user_tv_movie_data WHERE parent_tmdb=show_id AND "user"=user_email AND finished=true; SELECT count(*) INTO len_tv_episodes FROM rpiwebapp.tv_episodes WHERE parent_tmdb_id=show_id; IF len_user_data = len_tv_episodes THEN is_finished := true; END IF; RETURN is_finished; END $$;