title: Multiple inserts from multiple WITH/CTE
body: <p>I have a table like this:</p>
  
  <pre><code>CREATE TABLE mytable
  (
    col1 character varying(50),
    mydate timestamp without time zone
  );
  </code></pre>
  
  <p>I want to insert data to this table, but also I want to store the maximum id from my source:</p>
  
  <pre><code>insert into mytable (select myid, col1, mydate from sourcetable);
  </code></pre>
  
  <p>I don't have a myid  column in mytable, and I can't ask later something like this: <code>select max(myid) from sourcetable</code> because I'm getting a snapshot and the sourcetable is a transactional table (hundreds of new records by second) so I need to get the maximum id from that snapshot</p>
  
  <p>I tried something like this:</p>
  
  <pre><code>with query1 as (select myid, col1, mydate from sourcetable),
  query2 as (select max(myid) id from query1)
  insert into mytable (select co1, mydate from query1);
  update anothertable set value=(select myid from query2) where col2='avalue';
  </code></pre>
  
  <p>But I get this error:</p>
  
  <pre><code>ERROR:  relation "query2" does not exist
  LINE 1: update anothertable set value=(select myid from query2) wher...
  </code></pre>
  
  <p>Is there a way to solve this?</p>