How to generate unique hash-type id? - Mailing list pgsql-general

From Joe Kramer
Subject How to generate unique hash-type id?
Date
Msg-id b4c00a111001290020q1a9acfd8l5e699c890d0b7f45@mail.gmail.com
Whole thread Raw
Responses R: How to generate unique hash-type id?  (Vincenzo Romano <vincenzo.romano@notorand.it>)
Re: How to generate unique hash-type id?  (Adrian von Bidder <avbidder@fortytwo.ch>)
Re: How to generate unique hash-type id?  (hubert depesz lubaczewski <depesz@depesz.com>)
Re: How to generate unique hash-type id?  (Craig Ringer <craig@postnewspapers.com.au>)
List pgsql-general
Hello,

I need to generate unique id which is not guessable unlike
serial(integer) type. I need an id in format like md5 hash of random
number.
On top of that I need this id to be unique across multiple tables.

Anyone had to solve this problem before? Can you post any recipes or
best practices please?

My questions:

1. Avoiding collisions.
If I make an UNIQUE constraint and do generation of id triggered on
INSERT. What if collision happens? DO I nee d to check if unique hash
already exists and if not- regenerate.
This looks too primitive. Is there a readily available function or
methodology to do that?

2. Generating global unique id across multiple tables.
How to do that? My only idea is to have separate table to keep all
hashes and compare for collision against that table.
Is there a better way? Maybe by creating some special serial type that
is not integer but varchar?

3. what function to use to generate 64-bit random hash without much
overhead to CPU?

Thanks.

pgsql-general by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: Output float number with hex format
Next
From: Vincenzo Romano
Date:
Subject: R: How to generate unique hash-type id?