create SEQUENCE table_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; CREATE OR REPLACE FUNCTION "public"."snow_next_id"(OUT "result" int8) RETURNS "pg_catalog"."int8" AS $BODY$ DECLARE our_epoch bigint := 1314220021721; seq_id bigint; now_millis bigint; shard_id int := 5; BEGIN seq_id := nextval('table_id_seq') % 1024; 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; $BODY$ LANGUAGE plpgsql VOLATILE COST 100