I have a plpgsql function that takes one input parameter and returns multiple columns of data as a record.
CREATE FUNCTION my_func(
IN id bigint,
OUT foo bigint,
OUT bar double precision
)
RETURNS RECORD AS
$BODY$
BEGIN
-- some calculations
foo = id + 1;
bar = 42.0;
END;
$BODY$
LANGUAGE plpgsql;
I want to execute my_func
for each row of a table (table_input
) with the id column as input parameter and insert the returned row into another table (table_output
).
The table table_output
has the same columns as the columns return by my_func
.
How can I do that?
The query
SELECT my_func(id) INTO table_output FROM table_input;
gives me:
ERROR: column "my_func" has pseudo-type record
The following query does what I want, but my_func()
gets called twice per row:
INSERT INTO table_output
SELECT
(my_func(table_input.id)).foo,
(my_func(table_input.id)).bar
FROM table_input;
Any suggestions?
INSERT INTO table_output SELECT * FROM myfunc(id)
?