Re: How to generate unique hash-type id?

From: "Wappler, Robert" <rwappler(at)ophardt(dot)com>
To: "Joe Kramer" <cckramer(at)gmail(dot)com>, "Adrian von Bidder" <avbidder(at)fortytwo(dot)ch>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to generate unique hash-type id?
Date: 2010-01-29 12:13:17
Message-ID: C8E2DAF0E663A948840B04023E0DE32A0252362A@w2k3server02.de.ophardt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2010-01-29, Joe Kramer wrote:

> Thanks for the answer,
>
> I am unable to use ossp_uuid due to package install and/or server
> rebuild requirement.
>
> So I am trying to roll my own, and
> digest(quote_literal(random()+random()), 'sha256'), 'hex') doesn't
work:
>

Your input value is a random number, those aren't supposed to be unique
within any predictable number of iterations. Moreover I'd suspect, that
random() + random() significantly increases the probability to create
duplicated values due to the properties of the add-operation. In the
simplest example, the first invocation may return random number 3, the
second one random number 5 creating an input of 8 for the
digest-function. Another outcome of the random number generator may
yield 7 and 1 giving the same input.

I'd suggest to use some kind of sequence or something constructed from
the primary keys. But you may still see hash collisions although the
input is different.

--
Robert...

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2010-01-29 12:21:58 Re: Primary Key Increment Doesn't Seem Correct Under Table Partition
Previous Message Adrian von Bidder 2010-01-29 12:08:02 Re: How to generate unique hash-type id?