How can I generate a unique string per record in a table in Postgres?

前端 未结 6 2077
栀梦
栀梦 2020-12-06 04:57

Say I have a table like posts, which has typical columns like id, body, created_at. I\'d like to generate a unique string with the creation of each post, for use in somethin

6条回答
  •  情书的邮戳
    2020-12-06 05:05

    Use a Feistel network. This technique works efficiently to generate unique random-looking strings in constant time without any collision.

    For a version with about 2 billion possible strings (2^31) of 6 letters, see this answer.

    For a 63 bits version based on bigint (9223372036854775808 distinct possible values), see this other answer.

    You may change the round function as explained in the first answer to introduce a secret element to have your own series of strings (not guessable).

提交回复
热议问题