title: PostgreSQL: How to return dynamic rows from table without using "column definition list"?
body: <p>How to retrieve rows from table dynamically without using "column definition list"?  </p>
  
  <p>I am trying to do same by using polymorphic type "anyelement"(pseudo type) but getting error "structure of query does not match function result type". </p>
  
  <p>For example: I have table called "table1" which consist of following details.  </p>
  
  <p>--Table  </p>
  
  <pre><code>create table table1
  ( 
    slno integer,
    fname varchar,
    lname varchar,
    city varchar,
    country varchar
  )
  </code></pre>
  
  <p>--Function </p>
  
  <pre><code>create or replace function function1(column_name varchar,relation_name anyelement)
  returns setof anyelement as
  $fun$
  declare 
         cname varchar; 
         add_column varchar;
         group_column varchar;
         select_query varchar;
  begin
         if column_name='fname' then
            cname:=quote_ident(column_name);
            add_column:='"city"'||','||'"country"';
            group_column:='"slno"'||','||cname||','||'"city"'||','||'"country"';
  
         else 
            cname:=quote_ident(column_name);
            add_column:='"city"'||','||'"country"';
            group_column:='"slno"'||','||cname||','||'"city"'||','||'"country"';
  
         end if;
  
         select_query:='select slno,'||cname||','||add_column||' from '||pg_typeof(relation_name) || 'group by '||group_column;
  
         return query execute select_query;
  end;
  $fun$
  language plpgsql;
  </code></pre>
  
  <p>---Function call  </p>
  
  <pre><code>select * from function1('fname',NULL::table1);
  </code></pre>