We added a UUID column to our 80 million row DB and the default gets generated using the postgres uuid_generate_v4() function.
We backfilled the uuid using this script:
current = 1
batch_size = 1000
last_id = 80000000
while current < last_id
start_id = current
end_id = current + batch_size
puts "WORKING ON current: #{current}"
ActiveRecord::Base.connection.execute <<-SQL.squish
UPDATE table_name
SET public_id = uuid_generate_v4()
WHERE id BETWEEN '#{start_id}' and '#{end_id}' AND public_id IS NULL
SQL
current = end_id + 1
end
however, at the end of the script, we found that we had 135 duplicates, with some even having 3. How is this possible? Does the uuid_generate_v4() function generate dupes with such high probability?