title: Redshift insert simultaniously into mixing id table and another table
body: <h2>Situation</h2>
  
  <p>The output <code>out</code> table has values that come from some one of source tables, <code>source1</code> or <code>source2</code>. All these tables have unique <code>id</code> and there is another table <code>mix</code> that keeps the relation between these ids.</p>
  
  <h2>Problem</h2>
  
  <p>Given an update on <code>source1</code> with ids <code>sId</code>, I'd like to input values into <code>out</code> with new unique <code>newId</code>s and at the same time update <code>mix</code> table with <code>(newId, sId, source1)</code>. </p>
  
  <p>Is it incorrect to think that doing two step (below) is slower/less efficient?</p>
  
  <h2>Expectation</h2>
  
  <p>I'd like to input data into two tables at the same time since they're not dependent and want to know whether any of these fails. If it was my way it'd something like:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT
    s.id as sId, s.value as value, fn_uuid() as newId,'source1' as sourceName,
  INTO
    [mix (newId, sId, sourceName), out (newId, value)]
  FROM 
    source1 s;
  </code></pre>
  
  <h2>Current approach</h2>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT fn_uuid() as newId, id as sId, 'source1' as source INTO mix FROM source1
  </code></pre>
  
  <p>and then </p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT m.Id, s.value INTO out FROM source1 s JOIN mix m on s.id = m.sId
  </code></pre>
  
  <p>assuming that <code>fn_uuid()</code> <a href="https://stackoverflow.com/questions/46578889/how-to-generate-12-digit-unique-number-in-redshift">is defined as</a></p>
  
  <pre><code>CREATE OR REPLACE FUNCTION public.fn_uuid()
  RETURNS characte`r varying AS
  ' import uuid
   return uuid.uuid1().__str__()
   '
  LANGUAGE plpythonu VOLATILE;
  </code></pre>