Declarando a estrutura da tupla de um registro em PL / pgSQL
Não consigo encontrar nada na documentação do PostgreSQL que mostre como declarar um registro, ou linha, enquanto declaro a estrutura da tupla ao mesmo tempo. Se você não definir sua estrutura de tupla, receberá o erro "A estrutura da tupla de um registro ainda não atribuído é indeterminada".
Isso é o que estou fazendo agora, o que funciona bem, mas deve haver uma maneira melhor de fazê-lo.
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;