最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - How to set a seed for gen_random_uuid()? - Stack Overflow

programmeradmin6浏览0评论

For testing purposes on a table like this:

CREATE TABLE my_table (
    id   UUID DEFAULT gen_random_uuid() PRIMARY KEY,
    .
    .

...I would like to get the same id's on each test run to better compare test logs / test results.

Do there exist a function that works for gen_random_uuid() the same way setseed(x) works for random() ?

gen_random_uuid() returns version 4 UUIDs that does not include timestamps so I can't see why this shouldn't be possible. I guess I could always write my own my_gen_random_uuid() based on random() with some hex string trickery, but that would be much slower.

Example - the dice column always return the same values when running these two selects, the uuid column always varies but I don't want it to:

select setseed(1/7.0);
select floor(1+random()*6) dice, gen_random_uuid() from generate_series(1,7) order by 1,2;
 dice |           gen_random_uuid            
------+--------------------------------------
    2 | 27539fb8-da6d-4976-945e-1a84948b6d1f
    3 | 3969c800-70a2-48d0-b3e4-13edf02ee7fa
    3 | b68949b5-67b2-4786-b1f4-23654439e037
    3 | df1061d1-e890-4222-a4ea-d42c90d15435
    4 | ef1ce38e-1199-4101-9e40-a0eb930c492b
    5 | 81fe87fa-eab5-4229-b591-d83ae528353b
    6 | 54c61441-70e5-4d83-a08e-7b3662d20970
(7 rows)
发布评论

评论列表(0)

  1. 暂无评论