Skip to content

Instantly share code, notes, and snippets.

@nanom1t
Forked from brent-hoover/instragram_next_id.sql
Created October 16, 2017 07:42
Show Gist options
  • Save nanom1t/eaead126f1c4f28e1a47c7dc4f2ae545 to your computer and use it in GitHub Desktop.
Save nanom1t/eaead126f1c4f28e1a47c7dc4f2ae545 to your computer and use it in GitHub Desktop.
Instagram's sharding function. (see http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram for explanation)
CREATE OR REPLACE FUNCTION insta5.next_id(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 5;
BEGIN
SELECT nextval('insta5.table_id_seq') %% 1024 INTO seq_id;
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
result := (now_millis - our_epoch) << 23;
result := result | (shard_id << 10);
result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
CREATE TABLE insta5.our_table (
"id" bigint NOT NULL DEFAULT insta5.next_id(),
-- ...rest of table schema...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment