I have a function that returns a query based on this answer
It works fine except for the columns that are of type array
- result of array_agg
on tuples
Simplified Example
Note that in the real case the arrays result from a group by
subquery.
CREATE OR REPLACE FUNCTION get_array()
RETURNS TABLE (o_id int
, o_arr text[]
)
LANGUAGE plpgsql AS
$func$
BEGIN
RETURN QUERY
with dt as
(select 'x' col1, 'y' col2)
select 1 id,
(select array_agg((col1,col2)) from dt) arr;
END
$func$;
SELECT * FROM get_array();
Error
SQL Error [42804]: ERROR: structure of query does not match function result type
Detail: Returned type record[] does not match expected type text[] in column 2.
Where: SQL statement "with dt as
(select 'x' col1, 'y' col2)
select 1 id,
(select array_agg((col1,col2)) from dt) arr"
PL/pgSQL function get_array() line 3 at RETURN QUERY
How should I define the array column in the returning table type?
The query returns following data
with dt as
(select 'x' col1, 'y' col2)
select 1 id,
(select array_agg((col1,col2)) from dt) arr;
|id |arr |
|---|---------|
|1 |{"(x,y)"}|