title: Flip flopping data tables in Postgres
body: <p>I have a table of several million records which I am running a query against and inserting the results into another table which clients will query. This process takes about 20 seconds.</p>
  
  <p>How can I run this query, building this new table without impacting any of the clients that might be running queries against the target table?</p>
  
  <p>For instance. I'm running</p>
  
  <pre><code>BEGIN;
  DROP TABLE target_table;
  SELECT blah, blahX, blahY
  INTO target_table
  FROM source_table
  GROUP BY blahX, blahY
  COMMIT;
  </code></pre>
  
  <p>Which is then blocking queries to:</p>
  
  <pre><code>SELECT SUM(blah)
  FROM target_table
  WHERE blahX &gt; x
  </code></pre>
  
  <p>In the days of working with some SQL Server DBA's I recall them creating temporary tables, and then flipping these in over the current table.  Is this doable/practical in Postgres?</p>