title: Returning insert counts from multiple tables using postgres function
body: <p>I have a function which inserts 1 month worth data From main tables into History tables. As postgres does not have stored procedure I have declared the insert and delete logic as a function. I want to know if there is a way to return the counts from the inserts and deletes as an output of the function. Following is the code.</p>
  
  <pre><code>CREATE FUNCTION public.insertDelete(integer)
      RETURNS void
      LANGUAGE plpgsql
  
  AS $BODY$
  
  DECLARE
      saveTime timestamp;
  BEGIN
     saveTime := now();
  
  
  INSERT INTO public.hist_One
      (select * from public.main_One
      WHERE udate &lt; (saveTime - ($1::text || ' months')::interval));
  
  INSERT INTO public.hist_Two
      (select * from public.main_Two
      WHERE udate &lt; (saveTime - ($1::text || ' months')::interval));
  
  INSERT INTO public.hist_Three
      (select * from public.main_Three
      WHERE udate &lt; (saveTime - ($1::text || ' months')::interval));
  
  
  delete from public.main_One
  where udate &lt; (saveTime - ($1::text || ' months')::interval);
  
  delete from public.main_Two
  where udate &lt; (saveTime - ($1::text || ' months')::interval);
  
  delete from public.main_Three
  where udate &lt; (saveTime - ($1::text || ' months')::interval);
  
  
  END;
  $BODY$;
  </code></pre>
  
  <p>If I try to return Query then the code looks something like this </p>
  
  <pre><code>CREATE FUNCTION public.insertDelete(integer)
          RETURNS Table(Hist_One int, Main_one int)
          LANGUAGE plpgsql
  
      AS $BODY$
  
      DECLARE
          saveTime timestamp;
      BEGIN
         saveTime := now();
  
  
      INSERT INTO public.hist_One
          (select * from public.main_One
          WHERE udate &lt; (saveTime - ($1::text || ' months')::interval));
  
     RETURN QUERY select count(*) from public.main_One
          WHERE udate &lt; (saveTime - ($1::text || ' months')::interval)
  </code></pre>
  
  <p>The problem though is I want to return for all the tables and with <code>RETURN QUERY</code> I can do it only for one table. so How can I return a table that will list the inserts into the Hist tables and the deletes from Main tables ?</p>