Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Created January 11, 2025 03:32
Show Gist options
  • Save fabiolimace/1e1db35eaeff529e1d94b22362d11d86 to your computer and use it in GitHub Desktop.
Save fabiolimace/1e1db35eaeff529e1d94b22362d11d86 to your computer and use it in GitHub Desktop.
Pure SQL Function for Generating UUIDv7 on PostgreSQL
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
@lospejos
Copy link

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