title: How to use a record type variable in plpgsql?
body: <p>How can I use query result stored into a record type variable for another query within the same stored function? I use Postgres 9.4.4.</p>
  
  <p>With a table like this:</p>
  
  <pre><code>create table test (id int, tags text[]);
  insert into test values (1,'{a,b,c}'),
                          (2,'{c,d,e}');
  </code></pre>
  
  <p>I wrote a function (simplified) like below:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION func(_tbl regclass)
  RETURNS TABLE (t TEXT[], e TEXT[])
  LANGUAGE plpgsql AS $$
  DECLARE
    t RECORD;
    c INT;
  BEGIN
    EXECUTE format('SELECT id, tags FROM %s', _tbl) INTO t;
    SELECT count(*) FROM t INTO c;
    RAISE NOTICE '% results', c;
    SELECT * FROM t;
  END
  $$;
  </code></pre>
  
  <p>... but didn't work:</p>
  
  <pre><code>select func('test');
  </code></pre>
  
  <blockquote>
  <pre><code>ERROR:  42P01: relation "t" does not exist
  LINE 1: SELECT count(*) FROM t
                               ^
  QUERY:  SELECT count(*) FROM t
  CONTEXT:  PL/pgSQL function func(regclass) line 7 at SQL statement
  LOCATION:  parserOpenTable, parse_relation.c:986
  </code></pre>
  </blockquote>