How to generate random unique number in PostgreSQL using function -


in postgresql, how generate random unique integer number column, return not exits in table column?

see pseudo_encrypt function, implements permutation based on feistel network technique. combined postgres sequence, guarantees unicity of result, randomness human eye.

exemple:

create sequence seq maxvalue 2147483647;  create table tablename(  id bigint default pseudo_encrypt(nextval('seq')::int),  [other columns] ); 

the effective range of id here 0...2^32-1. can adjusted if necessary modifying function. variant 64 bits output space can found at: pseudo_encrypt() function in plpgsql takes bigint.


edit: pseudo_encrypt implements 1 permutation, , not accept user-supplied key. if prefer having own permutations, depending on secret keys, may consider skip32 (a 32-bit block cipher based on skipjack, 10 bytes wide keys).

a plpgsql function (ported perl/c) available at: https://wiki.postgresql.org/wiki/skip32


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -