title: PostgreSQL stored procedure with RETURNS TABLE(id integer) returning all NULLs
body: <p>I have a stored procedure in PostgreSQL 8.4 that calls another stored procedure depending on the integer value passed in as a parameter. Those stored procedures are called such that they should return a relation with one integer column. The problem I am having is that the outer stored procedure always returns a relation with the correct number of rows but with all of the id's NULL. </p>
  
  <p>Here is the stored procedure reduced to its simplest form:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION spa(count integer) 
  RETURNS TABLE (id integer) AS $$
  BEGIN
      RETURN QUERY SELECT generate_series(1, count);
  END;
  $$ LANGUAGE plpgsql;
  
  CREATE OR REPLACE FUNCTION spb(count integer) 
  RETURNS TABLE (id integer) AS $$
  BEGIN
      RETURN QUERY SELECT generate_series(1, count);
  END;
  $$ LANGUAGE plpgsql;
  
  CREATE OR REPLACE FUNCTION conditional_relation_return(objectType integer, count integer) 
  RETURNS TABLE (id integer) AS $$
  BEGIN
      IF objectType = 1 THEN
          RETURN QUERY SELECT id FROM spa(count);
      ELSIF objectType = 2 OR objectType = 3 THEN
          RETURN QUERY SELECT id FROM spb(count);
      END IF;
  
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>And if you call it:</p>
  
  <pre><code># select * from conditional_relation_return(1, 2);
   id 
  ----
  
  
  (2 rows)
  </code></pre>
  
  <p>Or more specifically:</p>
  
  <pre><code># select count(*) from conditional_relation_return(1, 2) where id is null;
   count 
  -------
       2
  (1 row)
  </code></pre>
  
  <p>But if you call one of the referenced stored procedures, you get the correct results:</p>
  
  <pre><code># select * from spa(2);
   id 
  ----
    1
    2
  (2 rows)
  </code></pre>
  
  <p>So why does conditional_relation_return return all NULLs?</p>