title: What's the easiest way to return a recordset from a PostgreSQL stored procedure?
body: <p>I simply have a table that contains a list of countries and their ISO country codes.  I'm wrapping the query in a stored procedure (aka function) such as:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION get_countries(
                      ) RETURNS setof record AS $$
          SELECT country_code, country_name FROM country_codes
  $$ LANGUAGE sql;
  </code></pre>
  
  <p>The error I am getting is:</p>
  
  <pre><code>ERROR:  a column definition list is required for functions returning "record"
  </code></pre>
  
  <p>I know that I can define a TYPE and then loop through the recordset like a cursor, but IIRC there's a better way to do this under newer versions of PostgreSQL (I'm using 8.4.3) but I'm pulling my hair out trying to remember.</p>
  
  <hr>
  
  <p>Edit: </p>
  
  <p>This works:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION get_countries(
                      ) RETURNS setof country_codes AS $$
          SELECT country_code, country_name FROM country_codes
  $$ LANGUAGE sql;
  </code></pre>
  
  <p>Note the "RETURNS setof [table name]". But it doesn't seem to be the most flexible.  It falls apart if I attempt to return a join of several tables.</p>