title: Input table for PL/pgSQL function
body: <p>I would like to use a plpgsql function with a table and several columns as input parameter. The idea is to split the table in chunks and do something with each part.</p>
  
  <p>I tried the following function:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION my_func(Integer)
    RETURNS SETOF my_part
  AS $$
  DECLARE
  out my_part;
  BEGIN
    FOR i IN 0..$1 LOOP
      FOR out IN
      SELECT * FROM my_func2(SELECT * FROM table1 WHERE id = i)
      LOOP
         RETURN NEXT out;
     END LOOP;
    END LOOP;
    RETURN;
  END;
  $$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p><code>my_func2()</code> is the function that does some work on each smaller part.</p>
  
  <pre><code>CREATE or REPLACE FUNCTION my_func2(table1) 
    RETURNS SETOF my_part2 AS
  $$ 
  BEGIN
  RETURN QUERY
  SELECT * FROM table1;
  END
  $$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p>If I run:</p>
  
  <pre><code>SELECT * FROM my_func(99);
  </code></pre>
  
  <p>I guess I should receive the first 99 IDs processed for each id.
  But it says there is an error for the following line:</p>
  
  <pre><code>SELECT * FROM my_func2(select * from table1 where id = i)
  </code></pre>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>The subquery is only allowed to return one column</p>
  </blockquote>
  
  <p>Why does this happen? Is there an easy way to fix this?</p>