title: How to call table function from another function in PostgreSQL?
body: <p>I am new to PostgreSQL.</p>
  
  <p>I have a function</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION func1 ( a_person_id integer) RETURNS TABLE (granted_group_id varchar(10)) AS $body$
  
            WITH v_granted_group_list AS (
            SELECT DISTINCT a.group_id 
                  FROM a_table a
                 )
          SELECT v.group_id FROM v_granted_group_list v;
  
  $body$
  LANGUAGE sql;
  </code></pre>
  
  <p>I want to use the output of this in other function <code>func2</code>.</p>
  
  <p>I tried to do that as in the following code and got an error.</p>
  
  <p>What can we use in place of the <code>v_access_groups</code> array?</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION func2 ( a_person_id IN integer,mhrc_emp_no IN varchar(50)) 
   RETURNS TABLE (granted_group_id varchar(10)) AS $body$
  DECLARE
  
     v_access_groups varchar[];
  BEGIN
  
        v_access_groups := func1(a_person_id);
        ---- this gives error
        ---ERROR:  malformed array literal: "LCCHG"
        ----DETAIL:  Array value must start with "{" or dimension information.
        --------CONTEXT:  PL/pgSQL function func2(integer,character varying) line 14 at SQL statement
  
        -- what can we you in place of v_access_groups array
      RETURN v_access_groups;
    END;
  $body$
  LANGUAGE PLPGSQL;
  </code></pre>
  
  <p>I want to use the result of <code>func2</code> in yet another function in a select query</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION func3 ( a_blurb_id integer, a_person_id integer,mhrc_emp_no varchar(20)) RETURNS boolean AS $body$
  DECLARE
  
       v_acc_count         numeric;
       v_accessAllowed     boolean:=FALSE;
  BEGIN
      SELECT COUNT(*) INTO v_acc_count
      FROM table1 agfa
      where  agfa.group_id IN (
          SELECT TO_CHAR(column_value) AS group_id
          FROM TABLE(func2(a_person_id,mhrc_emp_no))
      );
      RETURN TRUE;
    END;
  $body$
  LANGUAGE PLPGSQL;
  </code></pre>
  
  <p>How can I achieve this in PostgreSQL?</p>