title: Redshift: Serializable isolation violation on table
body: <p>I have a very large Redshift database that contains billions of rows of HTTP request data.</p>
  
  <p>I have a table called <code>requests</code> which has a few important fields:</p>
  
  <ul>
  <li><code>ip_address</code></li>
  <li><code>city</code></li>
  <li><code>state</code></li>
  <li><code>country</code></li>
  </ul>
  
  <p>I have a Python process running once per day, which grabs all distinct rows which have not yet been geocoded (do not have any city / state / country information), and then attempts to geocode each IP address via Google's Geocoding API.</p>
  
  <p>This process (pseudocode) looks like this:</p>
  
  <pre><code>for ip_address in ips_to_geocode:
      country, state, city = geocode_ip_address(ip_address)
      execute_transaction('''
          UPDATE requests
          SET ip_country = %s, ip_state = %s, ip_city = %s
          WHERE ip_address = %s
      ''')
  </code></pre>
  
  <p>When running this code, I often receive errors like the following:</p>
  
  <pre><code>psycopg2.InternalError: 1023
  DETAIL:  Serializable isolation violation on table - 108263, transactions forming the cycle are: 647671, 647682 (pid:23880)
  </code></pre>
  
  <p>I'm assuming this is because I have other processes constantly logging HTTP requests into my table, so when I attempt to execute my UPDATE statement, it is unable to select <em>all</em> rows with the ip address I'd like to update.</p>
  
  <p>My question is this: what can I do to update these records in a sane way that will stop failing regularly?</p>