If p_i < 0 is actually an error then you could raise an exception:
if p_i < 0 then
raise exception 'Don''t know what to do with %', p_i
end if;
If p_i < 0 should just quietly return nothing then you could do something like this:
create or replace function f1( p_i int ) returns table( c1 int ) as $$
begin
if p_i < 0 then
return;
end if;
return query select c2 from t1 where c1 = p_i;
end;
$$ language plpgsql;
From the fine manual:
39.6.1.2. RETURN NEXT and RETURN QUERY
[...]
the individual items to return are specified by a sequence of RETURN NEXT or RETURN QUERY commands, and then a final RETURN command with no argument is used to indicate that the function has finished executing.
Emphasis mine. So you can use your return query to return the query and just a simple return; to bail out without doing anything.
For example, the return; version gives me things like this:
=> select * from f1(-1);
c1
----
(0 rows)
=> select * from f1(1);
c1
----
1
1
...
(15 rows)
and the exception version does this:
=> select * from f1(-1);
ERROR: Don't know what to do with -1
TABLE()orSETOF ...you can simplyRETURN;. You must be thinking of functions that return a value and have noOUTparameter defined. Then you need to return a value. - Erwin Brandstetter