Skip to content

Instantly share code, notes, and snippets.

@joehenry087
joehenry087 / gist:275dc7525ecfe29df78641eec659cb3d
Created September 4, 2021 16:35
Creating pretty, concise and unique base 62 ids
// Not sure where I found this approach. Probably a combination of multiple kind people's advices.
// Basically we make a random number or something and encode it as 62 bit, for concise URL ids.
// I don't recall how uniqueness is guaranteed but I think its because we are basing the ID off the timestamp. You could also add a unique constraint on your DB column of course.
CREATE sequence global_id_sequence MINVALUE 0 MAXVALUE 1023 START 0 CYCLE;
CREATE OR REPLACE FUNCTION generate_number(OUT number_id bigint) AS $$
DECLARE
shard_id int := 1;
start_epoch bigint := 1498490095287;