PostgreSQL: turn record type output into table when using own function

793 views Asked by At

I have worked out the following function

CREATE OR REPLACE FUNCTION lgis_overview(id_number int) /*id of object*/
        RETURNS TABLE(datatype text, object_number int, object_name text,output text, link text)
    AS
    $$
    BEGIN
        RETURN QUERY
    (select tags.datatype::text, objects.id::int, objects.name::text, lgis_getvalue(tags.datatype::text, foo.id::int)::text, lgis_objectname(output)
    from tags, objects,(select * from values
    where object_id = id_number) as foo

    where tags.id = foo.tag_id
    and objects.id = foo.object_id);
   
END;
    $$
LANGUAGE 'plpgsql' VOLATILE;

This function gives this output:

select * from lgis_overview(148830)

enter image description here

Unfortunatelly, when i use this function in this form, the output is a list of record types:

select lgis_overview(id)
from objects 
limit 10

enter image description here

In what (clean) way can I rewrite the function so that the output is a table, or how should I use the function so that the output is a table?

1

There are 1 answers

0
AudioBubble On

Set returning functions should be used in the FROM clause (as you did in your first query).

If you need to pass it a column value, use a lateral join:

select ov.*
from objects as o
  cross join lateral lgis_overview(o.id) as ov
limit 10