You need to return setof record
(ref).
create or replace function foo_func(col1 text, col2 text) returns
setof record as
$$
declare
sql text;
r record;
begin
sql := 'select ' || col1 || ', ' || col2 || ' from foo';
for r in execute sql
loop
return next r;
end loop;
return;
end
$$ language plpgsql;
The problem is that you must declare the column types outside the function:
SELECT * from foo_func('value1','value2') as (a varchar(255),b int);
If the declaration mismatches you'll get an error:http://sqlfiddle.com/#!12/ce8b0/3