Postgresql 使用postgres uuid_generate_v4时有许多重复值

Postgresql 使用postgres uuid_generate_v4时有许多重复值,postgresql,activerecord,uuid,Postgresql,Activerecord,Uuid,我们在8000万行数据库中添加了一个UUID列,并使用postgresUUID\u generate\u v4()函数生成默认值 我们使用以下脚本回填uuid: 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}"

我们在8000万行数据库中添加了一个UUID列,并使用postgres
UUID\u generate\u v4()
函数生成默认值

我们使用以下脚本回填uuid:

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
current=1
批量大小=1000
最后一个id=80000000
当电流<最后一个id时
开始\u id=当前
结束id=当前+批次大小
将“当前工作:#{current}”

ActiveRecord::Base.connection.execute您正在使用哪个操作系统

根据ossp扩展使用/dev/uradom,因此它可能不会一直按预期工作。(我还没有检查索赔。)

您是否尝试过改用
gen\u random\u uuid()

因此,您可以尝试检查您的
/dev/uradom
,例如:

for i in $(seq 1 8000000); do uuidgen >>/tmp/u; done
-bash-4.2$ cat /tmp/u | sort | uniq -c | sort -r | head -3
      1 fffe894a-63e3-47e0-aea2-563f9652afd3
      1 fffbb781-61d5-4751-b4eb-e45a8ed684b7
      1 fffa7bff-ea37-46db-925b-d58f931512be
有点残酷,但是如果您在这里看到重复(左
1
将不止一个),您可能应该使用
uuid\u generate\u v1()
或其他不依赖于
/dev/uradom
或者另外使用一些时间戳,或者寻找其他解决方案。。。

可能值得检查您的主机,怀疑您的随机数来源。请看我们在heroku上,这是ubuntu。我们的postgres db已打开RDS@MatthewBerman然后我会向AWS寻求帮助。你在使用哪个PostgreSQL?我似乎无法在我的RDS PostgreSQL(9.5.4)上重现你的问题.我们使用的是PostgreSQL 9.4.7,根据RDS@MatthewBerman然后我建议您升级到最新的次要更新9.4.14
for i in $(seq 1 8000000); do uuidgen >>/tmp/u; done
-bash-4.2$ cat /tmp/u | sort | uniq -c | sort -r | head -3
      1 fffe894a-63e3-47e0-aea2-563f9652afd3
      1 fffbb781-61d5-4751-b4eb-e45a8ed684b7
      1 fffa7bff-ea37-46db-925b-d58f931512be