Skip to content

Instantly share code, notes, and snippets.

View nyku's full-sized avatar

Nicolae Rotaru nyku

  • Chisinau, Moldova
View GitHub Profile
@tcyrus
tcyrus / snowflake-id.sql
Last active May 25, 2021 09:20 — forked from beginor/snowflake-id.sql
Twitter Snowflake ID for PostgreSQL
CREATE SEQUENCE IF NOT EXISTS public.snowflake_id_seq
MINVALUE 0 MAXVALUE 1023
CYCLE;
ALTER SEQUENCE public.global_id_seq OWNER TO postgres;
-- 41 bits for time in milliseconds
-- (41 years of IDs from epoch)
-- 13 bits for the logical shard ID
-- 10 bits for the global auto-incrementing sequence (modulus 1024).
-- This means we can generate 1024 IDs, per shard, per millisecond
@beginor
beginor / snowflake-id.sql
Last active November 14, 2024 07:54
Twitter Snowflake ID for PostgreSQL
CREATE SEQUENCE public.global_id_seq;
ALTER SEQUENCE public.global_id_seq OWNER TO postgres;
CREATE OR REPLACE FUNCTION public.id_generator()
RETURNS bigint
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;