I have a Postgres function:
create function myfunction(integer, text, text, text, text, text, text) RETURNS table(id int, match text, score int, nr int, nr
If you can modify the query, you could do something like
and (ad_postcode = $4 OR $4 IS NULL)