title: Return setof record with 1 row
body: <p>I'm altering a <code>PLPGSQL</code> function and I'm having a small problem. First let me post it's declaration:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION permissions(_principal text)
    RETURNS SETOF record AS
  $BODY$
  DECLARE
      id integer := 0;
      rolerow record ;
  </code></pre>
  
  <p>In this function there are a few cases, controled by <code>IF</code> statements, and in all of them, the return is the <code>UNION</code> of more than 1 query, such as this:</p>
  
  <pre><code>FOR rolerow IN (
      (SELECT 'role1' AS role FROM table1 WHERE id = table1.id)
      UNION (SELECT 'role2' AS role FROM table2 WHERE id = table2.id)
      UNION (SELECT 'role3' AS role FROM table3 WHERE id = table3.id)
      ) 
  LOOP
      RETURN NEXT rolerow;
  END LOOP;
  RETURN;
  </code></pre>
  
  <p>And it all works fine, but in one case, I need to return a single query result, that would be a <code>SETOF record</code> but with only 1 item, so I did it like this:</p>
  
  <pre><code>FOR rolerow IN (
      SELECT 'role4' AS role FROM table4 WHERE id = table4.id
  ) 
  LOOP
      RETURN NEXT rolerow;
  END LOOP;
  RETURN;
  </code></pre>
  
  <p>I also tried</p>
  
  <pre><code>RETURN QUERY SELECT 'role4' AS role FROM table4 WHERE id = table4.id;
  </code></pre>
  
  <p>But in both cases I get the same error as a response:</p>
  
  <pre><code>ERROR:  structure of query does not match function result type
  DETAIL:  Returned type unknown does not match expected type text in column 1.
  </code></pre>
  
  <p>Does anyone have any idea how I can fix this?</p>
  
  <p>I'll provide extra information in case this isn't enough.</p>