I want to have an array of distinct integer values across my postgres table as the return value of a stored function.
The stored function currently looks like this
create or replace function get_unique_entries(id int)
returns table ("entry_id" int)
language plpgsql
as
$$
begin
return query
select distinct table.entry_id
from my_table
where x = id;
end;
$$;
When executing
select get_unique_entries(2);
, I get the following error message:
structure of query does not match function result type
I tried different return types, but nothing worked for me.
Thanks in advance!
2
Answers
While preparing the complete example I actually found it out myself.
As I am working with supabase, they display the datatype
BIGINT
asint8
. I was trying to set this as return type. Setting the return type toBIGINT
instead worked.So in general check I would recommend myself and to others to check your column data types exactly.
The working example looks like this (as indicated by @richyen)
Hmm, can you give us a more complete picture of your scenario? I tried using your code and it seems to work (except I needed to replace
table
withmy_table
):