-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Description
Hello, I created a pure pgSQL implementation of the time-based generation method.
Hopefully this is useful for those of us using a service like AWS RDS where we can't install C-based extensions.
It doesn't support customizing interval_count, but that should be trivial to add.
CREATE OR REPLACE FUNCTION generate_sequential_uuid(p_interval_length int DEFAULT 60)
RETURNS uuid
LANGUAGE plpgsql
AS $$
DECLARE
v_i int;
v_time bigint;
v_bytes int[16] = '{}';
v_hex text[16] = '{}';
BEGIN
v_time := floor(extract(epoch FROM clock_timestamp()) / p_interval_length);
v_bytes[1] := v_time >> 8 & 255;
v_bytes[2] := v_time & 255;
FOR v_i IN 3..16 LOOP
v_bytes[v_i] := floor(random() * 256);
END LOOP;
FOR v_i IN 1..16 LOOP
v_hex[v_i] := lpad(to_hex(v_bytes[v_i]), 2, '0');
END LOOP;
RETURN array_to_string(v_hex, '');
END $$;Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels