I am trying to call a function returning all columns of an existing table + some unrelated additional ones and retrieve the returned data using the following syntax:
select *
from test_func(...)
as (a_table my_table_name, rows_count numeric);
The function has the following format:
CREATE OR REPLACE FUNCTION public.test_func(...)
RETURNS SETOF record
LANGUAGE plpgsql
AS $function$
DECLARE
_sql VARCHAR;
begin
_sql := 'SELECT mtn.*, count(*) over() as rows_count
from public.my_table_name mtn
... inner joins and other stuff';
return query
execute _sql using ..._sql params...;
END;
$function$
;
However, it doesn't work. The error I receive:
ERROR: structure of query does not match function result type
Detail: Returned type numeric does not match expected type "my_table_name" in column 1.