title: plpgsql error "RETURN NEXT cannot have a parameter in function with OUT parameters" in table-returning function
body: <p>I have a plpgsql function in PostgreSQL 9.2 which returns a table. The function runs several SELECTs that return the same columns as the function and then either returns those results or raises an exception, depending on some checks. The only way I can see of doing this is with FOR ... LOOP, but I can't figure out a convenient way of returning the row.</p>
  
  <p>I want to do something like this:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION my_function()
  RETURNS TABLE(column1 integer, column2 boolean, ...)
  AS $BODY$
  DECLARE
      result_row RECORD;
  BEGIN
      FOR result_row IN (SELECT * FROM other_function_returning_same_columns()) LOOP
          IF something_wrong_with(result_row) THEN
              RAISE EXCEPTION 'Something went wrong';
          END IF;
  
          RETURN NEXT result_row;
      END LOOP;
  END
  $BODY$ LANGUAGE plpgsql STABLE;
  </code></pre>
  
  <p>This gives me an error: </p>
  
  <blockquote>
    <p>ERROR: RETURN NEXT cannot have a parameter in function with OUT parameters</p>
  </blockquote>
  
  <p>I'm not sure why Postgres is complaining here, because my code looks a lot like the example in the <a href="http://www.postgresql.org/docs/9.2/static/plpgsql-control-structures.html" rel="noreferrer">documentation</a>, except that my function returns TABLE instead of SETOF. There are no OUT parameters.</p>
  
  <p>I eventually managed to get it to work using</p>
  
  <pre><code>RETURN QUERY SELECT result_row.column1, result_row.column2, ...;
  </code></pre>
  
  <p>but having to list all the columns all the time is ugly and harder to maintain. I'm sure there must be a better way.</p>