Created
January 11, 2025 03:32
-
-
Save fabiolimace/1e1db35eaeff529e1d94b22362d11d86 to your computer and use it in GitHub Desktop.
Pure SQL Function for Generating UUIDv7 on PostgreSQL
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
CREATE OR REPLACE FUNCTION uuid7_sql() RETURNS uuid AS $$ | |
SELECT (FORMAT('%s-%s-%0s-%s-%s', | |
lpad(to_hex(trunc(EXTRACT(EPOCH FROM statement_timestamp()) * 1000)::bigint >> 16), 8, '0'), | |
lpad(to_hex(trunc(EXTRACT(EPOCH FROM statement_timestamp()) * 1000)::bigint & 65535), 4, '0'), | |
lpad(to_hex((trunc(random() * 2^12) + 28672)::bigint), 4, '0'), -- 28672 = 0x7000 | |
lpad(to_hex((trunc(random() * 2^14) + 32768)::bigint), 4, '0'), -- 32768 = 0x8000 | |
lpad(to_hex(trunc(random() * 2^48)::bigint), 12, '0')))::uuid; | |
$$ LANGUAGE SQL; | |
select uuid7_sql() -- 019450fe-7b0f-7ccc-8564-ad3ccc8234e0 |
Another script to check function, and this script results the same as in previous script:
WITH RECURSIVE uuid_generator AS (
SELECT
uuid7_sql()
--uuid7()
AS current_uuid,
1 AS iteration
UNION ALL
SELECT
uuid7_sql()
--uuid7()
,
iteration + 1
FROM uuid_generator
WHERE iteration < 1000000 -- Replace value with the desired number of UUIDs to generate
),
uuid_with_previous AS (
SELECT
current_uuid,
LAG(current_uuid) OVER (ORDER BY iteration) AS previous_uuid,
iteration
FROM uuid_generator
)
SELECT
current_uuid,
previous_uuid,
iteration
FROM uuid_with_previous
WHERE current_uuid < previous_uuid
LIMIT 1;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure but it seems this function generates not lexicographically ordered values, could you check it?
How I tested it: see script below.
If I use
uuid7_sql()
function, I get invalid rows (where next value is less then current).If I use
uuid7()
function from here: https://gist.github.com/fabiolimace/515a0440e3e40efeb234e12644a6a346#file-uuidv7-sql I get no invalid values.You can comment/uncomment functions to get and compare results.
I've tested this on PostgreSQL 14.16 on Windows 10.