title: pl/pgsql does not return all the results using record
body: <p>I am trying to create a complicated pl/pgsql function that gathers some results from a query and then checks each one and returns it or not.</p>
  
  <p>This is my code <strong>so far</strong>. The <strong><code>record</code> and <code>loop</code> part</strong> confuse me.</p>
  
  <pre><code>CREATE  FUNCTION __a_search_creator(creator text, ordertype integer, orderdate integer, areaid bigint) RETURNS record
  AS $$
  DECLARE    
  fromText text;
  whereText text;
  usingText text; 
  firstrecord record;
  areageom geometry;
  
  BEGIN
      IF areaid IS NOT NULL
      THEN        
          EXECUTE format('SELECT area.geom FROM area WHERE area.id=$1')
          INTO areageom
          USING areaid;  
  
          FOR firstrecord IN 
          EXECUTE format( 
          'SELECT place.id, person.name, place.geom 
          FROM '||fromText||'     
          WHERE '||whereText)  
          USING  creator, ordertype , orderdate         
          LOOP    
          --return only data that the place.geom is inside areageom using PostGIS 
          END LOOP;
      END IF;
      RETURN firstrecord;
  END;
  $$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p><strong>I plan</strong> to do some extra checks in the <code>loop</code>, as you can see and <code>RETURN</code> only data that the <code>place.geom</code> (lon/lat) is inside <code>areageom</code>. But since I am new to pl/pgsql, I am creating now <strong>the first step</strong>, just gathering all the data, put them in a <code>record</code> and return.</p>
  
  <p>My problem is that, no matter what I try I keep getting only one result back. I call <code>select  __a_search_creator('johnson',8, 19911109, 20);</code> and I get <code>1,"seth johnson", 65485,84545</code> but I know I should be getting another row of results. Is there overwrite happening? </p>
  
  <p>I tried putting <code>RETURN  NEXT firstrecord;</code> I tried something like </p>
  
  <pre><code>        select place.id from place INTO placeid;        
          select person.name from person INTO personname;
          select place.geom from place INTO placegeom;        
          firstrecord.id := placeid;
          firstrecord.name := personname;
          firstrecord.geom := placegeom;
  </code></pre>
  
  <p>That still brings back just one result, I tried testing just this <code>RAISE NOTICE '%', firstrecord.id;</code> that still brings back just one full set of result.</p>
  
  <p>I don't know how to proceed, please advice.</p>