title: Declaring the tuple structure of a record in PL/pgSQL
body: <p>I can't find anything in the PostgreSQL documentation that shows how to declare a record, or row, while declaring the tuple structure at the same time. If you don't define you tuple structure you get the error "The tuple structure of a not-yet-assigned record is indeterminate".</p>
  
  <p>This is what I'm doing now, which works fine, but there must be a better way to do it.</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION my_func()
    RETURNS TABLE (
      "a" integer,
      "b" varchar
    ) AS $$
  DECLARE r record;
  BEGIN
  
  CREATE TEMP TABLE tmp_t (
      "a" integer,
      "b" varchar
  );
  -- Define the tuple structure of r by SELECTing an empty row into it.
  -- Is there a more straight-forward way of doing this?
  SELECT * INTO r
  FROM tmp_t;
  
  -- Now I can assign values to the record.
  r.a := at.something FROM "another_table" at
         WHERE at.some_id = 1;
  
  -- A related question is - how do I return the single record 'r' from
  -- this function?
  -- This works:
  RETURN QUERY
  SELECT * FROM tmp_t;
  
  -- But this doesn't:
  RETURN r;
  -- ERROR:  RETURN cannot have a parameter in function returning set
  
  END; $$ LANGUAGE plpgsql;
  </code></pre>