in postgresql plpgsql,
create function f1( p_i int ) returns table( c1 int ) as $$
begin
-- wish to exit, do not wish to return anything
if p_i < 0 then
-- cannot RETURN - since can only return record!
end if;
-- continue processing
return query select c2 from t1 where c1 = p_i;
...
end;
$$ language plpgsql;
according to doc, the only way to break out of a function is RETURN. but a RETURN here requires RETURN QUERY or RETURN NEXT – there seems to be no way to simply exit out of the function.
If
p_i < 0is actually an error then you could raise an exception:If
p_i < 0should just quietly return nothing then you could do something like this:From the fine manual:
Emphasis mine. So you can use your
return queryto return the query and just a simplereturn;to bail out without doing anything.For example, the
return;version gives me things like this:and the exception version does this: