title: Synchronize two databases primary keys without atomicity
body: <p>Consider two databases A and B, with rows with a common primary key (PK). Both databases can only be queried by the function <code>return all keys of items [X,Y[</code>. The keys are non-sequential, and thus this query could return, e.g.</p>
  
  <pre><code>&gt;&gt;&gt; get_Akeys(0, 5)
  &gt;&gt;&gt; [20, 21, 23, 24, 25]
  &gt;&gt;&gt; get_Bkeys(0, 5)
  &gt;&gt;&gt; [20, 21, 23, 24, 25]
  </code></pre>
  
  <p>With time, database A changes (B does not): it has rows <strong>added</strong> (always with incremental PK) and rows <strong>deleted</strong> (any valid PK). The aim is to keep database B synchronised with A. To that, one would add to database B the rows <code>A - B</code> (set subtraction), and delete the rows <code>B - A</code>.</p>
  
  <p>Unfortunately, we don't have access to the full database A: queries of the form <code>get_Akeys(0, 10**10)</code> are forbidden because |A| is too large. Thus, a strategy is to chop it, e.g. <code>getA(i) = get_Akeys(i*N, (i + 1)*N)</code> where N is a constant, and query it sequentially.</p>
  
  <p>However, this breaks atomicity of the request: consider <code>A = [20,21,22,25,26,27,28]</code> and <code>B = [20,21,22,25,26,27]</code> with <code>N = 3</code> and this sequence of events:</p>
  
  <ol>
  <li><code>getA(0) == [20,21,22] == getB(0)</code> (all correct)</li>
  <li><code>A.delete_key(22) # getA(0) becomes [20,21,25]</code></li>
  <li><code>getA(1) == [26,27,28] != [25,26,27] == getB(1)</code></li>
  </ol>
  
  <p>In 3., the result would imply that the element 25 should be deleted from B (wrong), and element 28 should be added (correct).</p>
  
  <p>Any ideas on how to synchronise two databases with the above constraints?</p>
  
  <p>In my use case, each <code>getA</code> takes ~1 minute for the N I'm using, getting all entries requires ~100 queries for the N/|A| I'm using, and the sync can take 2 days to complete.</p>