This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |