title: How to do postgresql select query funciton using parameter?
body: <p>I want to create a postgresql funciton that returns records. But if I pass an id parameter, it should be add in where clause.  if I do not pass or null id parameter, where clasuse will not add the query.</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION my_func(id integer)
  RETURNS TABLE (type varchar, total bigint) AS $$
      DECLARE where_clause VARCHAR(200);
  BEGIN
      IF id IS NOT NULL THEN
          where_clause = ' group_id= ' || id;
      END IF ;
  
      RETURN QUERY SELECT
                 type,
                 count(*) AS total
           FROM                
                  table1
           WHERE
                 where_clause ???               
           GROUP BY
                     type
           ORDER BY
                     type;
  END
  $$
  LANGUAGE plpgsql;
  </code></pre>