title: PostgreSQL: apply return of query to a function
body: <p>I have a recursive method defined below:</p>
  
  <pre><code>with recursive temp(id, s, r, e) as (
      select *
      from rel
      where rel_to_id = &lt;parameter from sql query&gt;
  
      union all
  
      select *
      from temp P
      inner join relationship C on P.r = C.s
  )
  </code></pre>
  
  <p>and I need to call this on each row returned from an SQL query with a column value defined in the recursive query (marked as ) </p>
  
  <p>I dont really want to call X queries through python which slows things down, there must be a way to do it in sql. I tried to write a function in plpgsql but I am having trouble defining the return type setof TABLE and taking the union of it each time.</p>