1

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.
2
  • Instead of SELECT mtn.*, use the actual column name that contains the table name Commented Aug 27, 2021 at 9:49
  • @Shameel I don't understand. How do I select all the table columns then? Commented Aug 27, 2021 at 9:54

1 Answer 1

1

If the query in your function should return a my_table_name (a “whole-row reference), you should write it like

SELECT mtn, count(*) OVER () ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.