title: querying result of a function which inserts data and returns set of ids
body: <p>I have a plpgsql function that returns table with and id column which represents all inserted data inside function. something like this:</p>
  
  <pre class="lang-sql prettyprint-override"><code>create or replace function se_initial_games("tournamentId_" bigint)
  returns table(
    "gameId" bigint
  ) as $$
  declare
    n_teams bigint;
  begin
    return query
    insert into single_elimination_tournaments_games (team_one_id, team_two_id, tournament_id)
    (select ...) returning id;
  end;
  $$ language plpgsql;
  </code></pre>
  
  <p>It is returning results when running</p>
  
  <pre class="lang-sql prettyprint-override"><code>select "gameId" from se_initial_games(1);
  </code></pre>
  
  <p>but not if I use it in a subquery e.g:</p>
  
  <pre class="lang-sql prettyprint-override"><code>select g.* from games g where g.id in (select "gameId" as id from se_initial_games(1));
  </code></pre>
  
  <p>Something tells me function results after games table is read.</p>
  
  <p>The problem is that the results of query <code>select g.* from games g g.id</code> don't include newly inserted ids from <code>in</code> subquery.</p>
  
  <p>I ended up writing 2 queries for this.</p>