Skip to content

Instantly share code, notes, and snippets.

View windofwind's full-sized avatar
🤑

dev Kim windofwind

🤑
View GitHub Profile
@windofwind
windofwind / UUID_v7_for_Postgres.sql
Created November 2, 2023 05:00 — forked from joaocc/UUID_v7_for_Postgres.sql
Postgres PL/pgSQL function for UUID v7 and a bonus custom UUID v8 to support microsecond precision as well. Read more here: https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
-- Based off IETF draft, https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
create or replace function uuid_generate_v7()
returns uuid
as $$
declare
unix_ts_ms bytea;
uuid_bytes bytea;
begin
unix_ts_ms = substring(int8send(floor(extract(epoch from clock_timestamp()) * 1000)::bigint) from 3);