Make a non-ordinal numeric ID in PostgreSQL If I declare a column to be serial in PostgreSql, it will auto-increment in a ordinal sequence.  The end result may not be contigious due to rollbacks and whatnot, but it's more or less ordinal.

I have a user table where the user id's are set as serial, and are thus incremented starting from 1... but I'd rather they weren't, as it seems like asking for a sequence number attack of some kind.  

Is there a relatively easy, relatively performant way to create an automatic artificial ID in PostgreSQL such that the ID's are randomly spaced apart, and you shouldn't expect that ID 987654321 follows ID 987654320?
but I'd rather they weren't, as it seems like asking for a sequence number attack of some kind


Aside from the fact that I think you are giving yourself a false sense of security, you can do what you want by reserving the upper half of the identifier for the 'real' sequence and filling the lower half with random bits:




create sequence seq;






create table tab(
  foo bigint 
      default (nextval('seq')&lt;&lt;32)+(random()*(1&lt;&lt;32))::integer
      primary key
);






insert into tab select from generate_series(1,9);






select foo, foo&gt;&gt;32 seq from tab;



        foo | seq
----------: | --:
 4294967296 |   1
 8589934592 |   2
12884901889 |   3
17179869185 |   4
21474836481 |   5
25769803777 |   6
30064771073 |   7
34359738369 |   8
38654705664 |   9



dbfiddle here