pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
HOWTO - Random character generation for primary key
5+ messages / 3 participants
[nested] [flat]

* HOWTO - Random character generation for primary key
@ 2002-05-02 09:04  Jean-Michel POURE <jm.poure@freesurf.fr>
  0 siblings, 1 reply; 5+ messages in thread

From: Jean-Michel POURE @ 2002-05-02 09:04 UTC (permalink / raw)
  To: Alan Wayne <alanjwayne@yahoo.com>; Stephan Szabo <sszabo@megazone23.bigpanda.com>; +Cc: pgsql-admin

Le Jeudi 2 Mai 2002 04:01, Alan Wayne a écrit :
> In foxpro, I've been using the provided 10-character
> random generator to provide for the primary keys on my
> data files. As far as I can tell, PostgreSQL only uses
> an auto-incrementing serial field for its primary
> keys. So as far as I can see, I'm left with the
> options of somehow converting all my existing records
> (with their relationships intact) to some form of
> integer key, or providing a similiar random character
> generator from PostgreSQL. So what to do????

Dear Alan,

Do not hesitate to CC pgsql-general@postgresql.org and pgsql-admin so that 
anyone can participate.

I don't know if plain SQL supports loops, I guess not (?). Writing a primary 
key generator is quite easy under PostgreSQL using PLpgSQL. So let's go for 
PLpgSQL:

First of all, enable PLpgSQL in your database:

Under postgresql user:
postgres@locahost>createlang plpgsql database_name

Then add this PLpgSQL script to your database:

CREATE FUNCTION "random_string"("int4") RETURNS "varchar" AS '
DECLARE
iLoop int4;
result varchar;

BEGIN
result = '''';
IF ($1>0) AND ($1 < 255) THEN
  FOR iLoop in 1 .. $1 LOOP
    result = result || chr(int4(random()*26)+65);
  END LOOP;
  RETURN result;
ELSE
  RETURN ''f'';
END IF;
END;
'  LANGUAGE 'plpgsql';

chr() is multi-byte safe. This means it will work for an UTF-8 or ASCII 
database. Not all PostgreSQL system functions are commented and described. A 
good way to learn how to use these functions is to run pgAdmin2 
(http://pgadmin.postgresql.org) and choose the "'display system objects" 
option. Each function has a small comment which provides a small description. 

PLpgSQL syntax is close to Pascal and SQL. More information can be found on:
http://www.postgresql.org/idocs/index.php?programmer-pl.html

PostgreSQL advantage compared to other open-source systems, like MySQL for 
example, is the ability to write server-side code.

It is highliy recommanded to migrate some of your existing Foxpro / Access 
code to PostgreSQL server-side, for the following reasons:

- a server-side application has a better transactional behavior (ex: perform 
complex actions when you add / drop / update a table within transactions).

- a PostgreSQL server-side application is ***much faster*** than a flat table 
application (PostgreSQL is able to process complex scripts involving several 
tables in ONE client-side query. In some situations, it can boost the speed 
by a 10 to 100 factor).

After migration, your Foxpro / Access application should look like a simple 
data "viewer" or "browser" application. Furthermore, it allows you to add a 
Web interface (ex: PHP) quite easily because, again, PHP will be used for 
data viewing, not data processing.

If you need more information, do not hesitate to get back to me and/or post 
comments on the list.

Cheers,
Jean-Michel



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOWTO - Random character generation for primary key
@ 2002-05-02 09:30  postgres@vrane.com
  parent: Jean-Michel POURE <jm.poure@freesurf.fr>
  0 siblings, 1 reply; 5+ messages in thread

From: postgres@vrane.com @ 2002-05-02 09:30 UTC (permalink / raw)
  To: alanjwayne@yahoo.com; +Cc: pgsql-general@postgresql.org

> Le Jeudi 2 Mai 2002 04:01, Alan Wayne a écrit :
> > data files. As far as I can tell, PostgreSQL only uses
> > an auto-incrementing serial field for its primary
> > keys. So as far as I can see, I'm left with the

Untrue.  I am using email addresses
as primary keys in a table.  Where
did you get that information?




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOWTO - Random character generation for primary key
@ 2002-05-02 09:58  Jean-Michel POURE <jm.poure@freesurf.fr>
  parent: postgres@vrane.com
  0 siblings, 1 reply; 5+ messages in thread

From: Jean-Michel POURE @ 2002-05-02 09:58 UTC (permalink / raw)
  To: postgres@vrane.com; alanjwayne@yahoo.com; +Cc: pgsql-general@postgresql.org

Le Jeudi 2 Mai 2002 11:30, postgres@vrane.com a écrit :
> Untrue.  I am using email addresses
> as primary keys in a table.  Where
> did you get that information?

Sorry, I did not mean that of course. I posted this small howto in reply to a 
mail. It shows how to create a VACHAR PRIMARY KEY with unique random values.

On 10 character lenght, there is approximatively one chance out of 26^10 to 
have a similar value. If you add a string timestamp, like "20020501_1153_" in 
front of the random value, it is very likely that this value will be UNIQUE 
in the word:

Examples:
- 20020501_1153_AHFYRIDKRN
- 20020501_1154_JDIFTPWNEJ

I would like to write a small HOWTOs about :
- Software optimization and performance (not pseudo-hardware / memory /  
etc..),
- PLpgSQL and PLpython. 

Therefore, I try to send small HOWTOs on the list whenever I can answer 
someone. Any help welcome.

Best regards,
Jean-Michel POURE



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOWTO - Random character generation for primary key
@ 2002-05-02 11:01  Jean-Michel POURE <jm.poure@freesurf.fr>
  parent: Jean-Michel POURE <jm.poure@freesurf.fr>
  0 siblings, 1 reply; 5+ messages in thread

From: Jean-Michel POURE @ 2002-05-02 11:01 UTC (permalink / raw)
  To: postgres@vrane.com; +Cc: pgsql-general@postgresql.org

Le Jeudi 2 Mai 2002 12:13, postgres@vrane.com a écrit :
> > Le Jeudi 2 Mai 2002 11:30, postgres@vrane.com a écrit :
> > > Untrue.  I am using email addresses
> > > as primary keys in a table.  Where
> > > did you get that information?
> > 
> > Sorry, I did not mean that of course. I posted this small howto in reply
> > to a mail. It shows how to create a VACHAR PRIMARY KEY with unique random
> > values. 
> > On 10 character lenght, there is approximatively one chance out of 26^10
> > to have a similar value. If you add a string timestamp, like
> > "20020501_1153_" in front of the random value, it is very likely that
> > this value will be UNIQUE in the word:
>
> In the real world "very likely" is not good enough.  Can you gurantee
> 'uniqueness'?

A timestamp (ex: 20020501_1258) followed by PLpgSQL function 
random_string(200) value should provide a unique signature. What do you 
think? Alternatively, could we use the crypto package to generate a unique 
signature?

Cheers,
Jean-Michel POURE



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOWTO - Random character generation for primary key
@ 2002-05-02 15:34  Bruno Wolff III <bruno@wolff.to>
  parent: Jean-Michel POURE <jm.poure@freesurf.fr>
  0 siblings, 0 replies; 5+ messages in thread

From: Bruno Wolff III @ 2002-05-02 15:34 UTC (permalink / raw)
  To: Jean-Michel POURE <jm.poure@freesurf.fr>; +Cc: postgres@vrane.com; pgsql-general@postgresql.org

On Thu, May 02, 2002 at 01:01:22PM +0200,
  Jean-Michel POURE <jm.poure@freesurf.fr> wrote:
> 
> A timestamp (ex: 20020501_1258) followed by PLpgSQL function 
> random_string(200) value should provide a unique signature. What do you 
> think? Alternatively, could we use the crypto package to generate a unique 
> signature?

Well what are you really trying to do? Presumably you have some reason(s)
for not wanting to use sequences, but without knowing what the reason(s)
are it is hard to give you good advice.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2002-05-02 15:34 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-05-02 09:04 HOWTO - Random character generation for primary key Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-02 09:30 ` postgres@vrane.com
2002-05-02 09:58   ` Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-02 11:01     ` Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-02 15:34       ` Bruno Wolff III <bruno@wolff.to>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox