title: Optional argument in PL/pgSQL function
body: <p>I am trying to write a PL/pgSQL function with optional arguments. It performs a query based on a filtered set of records (if specified), otherwise performs a query on the entire data set in a table.</p>
  
  <p>For example <strong>(PSEUDO CODE)</strong>:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION foofunc(param1 integer, param2 date, param2 date, optional_list_of_ids=[]) RETURNS SETOF RECORD AS $$
      IF len(optional_list_of_ids) &gt; 0 THEN
          RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2 AND id in optional_list_of_ids);
      ELSE
          RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2);
      ENDIF
  $$ LANGUAGE SQL;
  </code></pre>
  
  <p>What would be the correct way to implement this function?</p>
  
  <p>As an aside, I would like to know how I could call such a function in another outer function. This is how I would do it - is it correct, or is there a better way?</p>
  
  <pre><code>CREATE FUNCTION foofuncwrapper(param1 integer, param2 date, param2 date) RETURNS SETOF RECORD AS $$
  BEGIN
     CREATE TABLE ids AS SELECT id from foobar where id &lt; 100;
     RETURN QUERY (SELECT * FROM foofunc(param1, param2, ids));
  END
  $$ LANGUAGE SQL
  </code></pre>