This SQL creates a Postgres function to generate sequential, numeric, unique IDs in a consistent format across services. Useful for database sharding or microservices.
Draws heavily on Instagram's ID generator, via Rob Conery, with minor modifications.
The main changes are that the unique number resolution is per-second rather than per-millisecond. This is to reduce key size below 2^53^-1 so that generated IDs that are under Javascripts Number.MAX_SAFE_INTEGER
limit . This is important if you're using these on a Node.js server (e.g. our use case is an Express API using Hashids).
Max IDs are in the order of 51 bits, broken down as follows:
- 31 bits for the timestamp difference