Re: Random strings

From: "Joe Conway" <joseph(dot)conway(at)home(dot)com>
To: "Peter Eisentraut" <peter_e(at)gmx(dot)net>
Cc: "Dr(dot) Evil" <drevil(at)sidereal(dot)kz>, <pgsql-patches(at)postgresql(dot)org>
Subject: Re: Random strings
Date: 2001-08-08 20:25:22
Message-ID: 02ac01c12048$40312020$48d210ac@jecw2k1
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-patches

> > Per the discussion yesterday, here's a patch. There are two versions of
> > essentially the same function. They both take an int as the number of
> > requested random bytes, and generate a random binary string of the
requested
> > length from /dev/urandom. The first one (randomstr_hex) converts the
binary
> > to hex and returns it as text, and the other (randomstr_bytea) does the
> > needed escaping of special characters and returns bytea.
>
> Perhaps one of these returning bytea would be enough and you can use
> the new encode functions to convert them to a format of choice. Also, why
> aren't you using /dev/random?

Very nice. I hadn't looked encode/decode yet. I'll rework the patch and just
leave the bytea as you suggested.

As far as /dev/random, the issue it that it blocks when it runs out of
entropy. As a small test, I recompiled with /dev/random and tried:

select randomstr_bytea(8), foo from bar;

where bar has about 120 rows. I was still waiting a minute later. When
requesting one at a time (but resubmitting relatively quickly), it works
fine until about the 35th request in a row, then it blocks briefly (~2
seconds). The same test with /dev/urandom returns instantly. Perhaps there
should be an option to use either. For instances where only a few truly
random bytes is needed (i.e. one session key), use /dev/random. When you
need many random bytes quickly, use /dev/urandom?

-- Joe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Wheeler 2001-08-08 20:34:12 Re: Long Query
Previous Message Bruce Momjian 2001-08-08 19:21:53 Re: [GENERAL] Bug with ALTER TABLE

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2001-08-08 20:42:07 Re: [PATCHES] JDBC Statement cleanup patch
Previous Message Anders Bengtsson 2001-08-08 20:03:40 JDBC Statement cleanup patch