"ownership" of sequences, pseudo random unique id

Started by Ivan Sergio Borgonovoover 16 years ago4 messagesgeneral
Jump to latest
#1Ivan Sergio Borgonovo
mail@webthatworks.it

I've

create table pr(
code varchar(16) primary key,
...
);
create sequence pr_code_seq owned by pr.code; -- uh!

pr.code will *mostly* be obtained as

to_hex(feistel_encrypt(nextval('pr_code')))
and sometimes 'manually' inserting unique codes.

actually stuff like:
alter table pr drop column code;
or just
drop table pr

seems to work as expected (they drop the sequence too).

Should I be concerned of anything since it looks like a hack?

--
Ivan Sergio Borgonovo
http://www.webthatworks.it

#2Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Ivan Sergio Borgonovo (#1)
Re: "ownership" of sequences, pseudo random unique id

Ivan Sergio Borgonovo wrote:

I've

create table pr(
code varchar(16) primary key,
...
);
create sequence pr_code_seq owned by pr.code; -- uh!

pr.code will *mostly* be obtained as

to_hex(feistel_encrypt(nextval('pr_code')))
and sometimes 'manually' inserting unique codes.

actually stuff like:
alter table pr drop column code;
or just
drop table pr

seems to work as expected (they drop the sequence too).

Should I be concerned of anything since it looks like a hack?

You need to ensure you have a retry loop in your insertion code, because
if the generated code conflicts with a manually inserted code, it will
cause an error. Other than that, seems like it should work ...

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

#3Ivan Sergio Borgonovo
mail@webthatworks.it
In reply to: Alvaro Herrera (#2)
Re: "ownership" of sequences, pseudo random unique id

On Thu, 20 Aug 2009 14:31:02 -0400
Alvaro Herrera <alvherre@commandprompt.com> wrote:

Ivan Sergio Borgonovo wrote:

I've

create table pr(
code varchar(16) primary key,
...
);
create sequence pr_code_seq owned by pr.code; -- uh!

actually stuff like:
alter table pr drop column code;
or just
drop table pr

seems to work as expected (they drop the sequence too).

Should I be concerned of anything since it looks like a hack?

You need to ensure you have a retry loop in your insertion code,
because if the generated code conflicts with a manually inserted
code, it will cause an error. Other than that, seems like it
should work ...

I was mainly concerned about assigning ownership of a sequence to a
column that is not an int.
This looks like an hack ;) but it looks to work as expected:
dropping the column or the table drop the sequence.
Assigning ownership just avoid me to remember that if I drop the
column I don't need the sequence.
So owned by just mean "drop if" regardless of type or anything else.

I could even define the table as
create table pr(
code varchar(16) primary key,
...
);
create sequence pr_code_seq owned by pr.code;
alter table pr
alter column code
set default
to_hex(feistel_encrypt(nextval('pr_code_seq')));
That will make more explicit the relationship between the sequence
and the column.

I think I can avoid conflict between auto and manually generated
codes imposing a different format on input in client code.

thanks

--
Ivan Sergio Borgonovo
http://www.webthatworks.it

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ivan Sergio Borgonovo (#3)
Re: "ownership" of sequences, pseudo random unique id

Ivan Sergio Borgonovo <mail@webthatworks.it> writes:

I was mainly concerned about assigning ownership of a sequence to a
column that is not an int.

I think you can get away with that. It's already the case within the
standard usage that the owning column could be either int or bigint.

FWIW, I agree with the idea of adding a default expression so that it
behaves even more like a regular serial column.

regards, tom lane