0

I am writing a simple postgres function. The result of this function should be a row of the "events" table. I have the following:

create or replace function featured_event() returns setof events as
$$
begin
select events.* from events where featured is true;
end
$$ LANGUAGE plpgsql; 

I don't want to hardcode the columns, but instead use the structure from the existing table as the return type.

This is not a duplicate of Function with SQL query has no destination for result data as I do not want to use the table result type.

Community
  • 1
  • 1
bcardarella
  • 4,667
  • 4
  • 29
  • 45

1 Answers1

2

Use SQL function:

create or replace function featured_event() returns setof events as
$$
    select events.* from events where featured is true;
$$ LANGUAGE sql; 

In plpgsql you should use return query:

create or replace function featured_event_plpgsql() returns setof events as
$$
begin
    return query select events.* from events where featured is true;
end;
$$ LANGUAGE plpgsql; 
klin
  • 112,967
  • 15
  • 204
  • 232