Skip to content

Commit

Permalink
Add support for more time precision per latest RFC spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr committed Sep 11, 2023
1 parent ce889a8 commit c0c7bdb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "pg_uuidv7",
"abstract": "Create UUIDv7 values in Postgres",
"version": "1.2.0",
"version": "1.3.0",
"maintainer": "fboulnois <fboulnois@users.noreply.github.com>",
"license": "open_source",
"provides": {
"pg_uuidv7": {
"abstract": "Create UUIDv7 values in Postgres",
"file": "pg_uuidv7--1.2.sql",
"file": "pg_uuidv7--1.3.sql",
"docfile": "README.md",
"version": "1.2.0"
"version": "1.3.0"
}
},
"resources": {
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MODULES = pg_uuidv7
EXTENSION = pg_uuidv7
DATA = pg_uuidv7--1.2.sql
DATA = pg_uuidv7--1.3.sql

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
Expand Down
20 changes: 20 additions & 0 deletions pg_uuidv7--1.3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use '''CREATE EXTENSION "pg_uuidv7"''' to load this file. \quit

-- 48 bits for ms since unix epoch (rollover in 10899), 74 bits of randomness
CREATE FUNCTION uuid_generate_v7(extra_ts_precision int = 0)
RETURNS uuid
AS 'MODULE_PATHNAME', 'uuid_generate_v7'
VOLATILE STRICT LANGUAGE C PARALLEL SAFE;

-- extract the timestamp from a v7 uuid
CREATE FUNCTION uuid_v7_to_timestamptz(uuid)
RETURNS timestamptz
AS 'MODULE_PATHNAME', 'uuid_v7_to_timestamptz'
VOLATILE STRICT LANGUAGE C PARALLEL SAFE;

-- create a v7 uuid from a timestamp
CREATE FUNCTION uuid_timestamptz_to_v7(timestamptz, zero bool = false)
RETURNS uuid
AS 'MODULE_PATHNAME', 'uuid_timestamptz_to_v7'
VOLATILE STRICT LANGUAGE C PARALLEL SAFE;
11 changes: 11 additions & 0 deletions pg_uuidv7.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ PG_FUNCTION_INFO_V1(uuid_generate_v7);
Datum uuid_generate_v7(PG_FUNCTION_ARGS)
{
pg_uuid_t *uuid = palloc(UUID_LEN);
uint8_t extra_ts_p = 0;
struct timespec ts;
uint64_t tms;

if (!PG_ARGISNULL(0))
extra_ts_p = PG_GETARG_INT32(0);

if (!pg_strong_random(uuid, UUID_LEN))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
Expand All @@ -39,6 +43,13 @@ Datum uuid_generate_v7(PG_FUNCTION_ARGS)
tms = pg_hton64(tms << 16);
memcpy(&uuid->data[0], &tms, 6);

if (extra_ts_p > 1 && extra_ts_p <= 12) {
tms = (((uint64_t)ts.tv_nsec << extra_ts_p) / 1000000)
& ((1 << extra_ts_p) - 1);
tms = pg_hton64(tms << 48);
memcpy(&uuid->data[6], &tms, 2);
}

/*
* Set magic numbers for a "version 7" UUID, see
* https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-00.html#name-uuid-version-7
Expand Down
2 changes: 1 addition & 1 deletion pg_uuidv7.control
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
comment = 'pg_uuidv7: create UUIDv7 values in postgres'
default_version = '1.2'
default_version = '1.3'
module_pathname = '$libdir/pg_uuidv7'
relocatable = true

0 comments on commit c0c7bdb

Please sign in to comment.