diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 3af0a615c04..b57f161f53c 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -14465,6 +14465,12 @@ CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple sub-millisecond timestamp + random. The optional parameter shift will shift the computed timestamp by the given interval. + Infinite interval values are not accepted. + The shifted timestamp must fall within the range supported by + UUID version 7's 48-bit millisecond timestamp field: from + 1970-01-01 00:00:00 UTC to approximately year 10889. + An error is raised if the resulting timestamp is outside this + range. uuidv7() diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c index a746388b073..c910836da06 100644 --- a/src/backend/utils/adt/uuid.c +++ b/src/backend/utils/adt/uuid.c @@ -33,6 +33,23 @@ #define NS_PER_US INT64CONST(1000) #define US_PER_MS INT64CONST(1000) +/* + * The offset between the PostgreSQL epoch (2000-01-01) and the Unix epoch + * (1970-01-01) in microseconds. Subtract this from a Unix-epoch microseconds + * to get a TimestampTz. + */ +#define PG_UNIX_EPOCH_OFFSET_US \ + ((int64) (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC) + +/* + * Valid timestamp range for UUID version 7, expressed in PostgreSQL-epoch + * microseconds. UUIDv7 uses a 48-bit unsigned millisecond field relative + * to the Unix epoch, so the representable window is [1970-01-01, ~10889]. + */ +#define UUIDV7_MIN_TIMESTAMP (-PG_UNIX_EPOCH_OFFSET_US) +#define UUIDV7_MAX_TIMESTAMP \ + (((INT64CONST(1) << 48) - 1) * US_PER_MS - PG_UNIX_EPOCH_OFFSET_US) + /* * UUID version 7 uses 12 bits in "rand_a" to store 1/4096 (or 2^12) fractions of * sub-millisecond. While most Unix-like platforms provide nanosecond-precision @@ -676,6 +693,13 @@ uuidv7_interval(PG_FUNCTION_ARGS) int64 ns = get_real_time_ns_ascending(); int64 us; + /* Reject infinite intervals before any arithmetic */ + if (INTERVAL_NOT_FINITE(shift)) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("interval out of range for UUID version 7"), + errdetail("UUID version 7 does not support infinite intervals."))); + /* * Shift the current timestamp by the given interval. To calculate time * shift correctly, we convert the UNIX epoch to TimestampTz and use @@ -683,16 +707,26 @@ uuidv7_interval(PG_FUNCTION_ARGS) * precision. */ - ts = (TimestampTz) (ns / NS_PER_US) - - (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC; + ts = (TimestampTz) (ns / NS_PER_US) - PG_UNIX_EPOCH_OFFSET_US; /* Compute time shift */ ts = DatumGetTimestampTz(DirectFunctionCall2(timestamptz_pl_interval, TimestampTzGetDatum(ts), IntervalPGetDatum(shift))); - /* Convert a TimestampTz value back to an UNIX epoch timestamp */ - us = ts + (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC; + /* + * Reject timestamps outside the range representable by UUID version 7's + * 48-bit millisecond field. We compare in PostgreSQL-epoch units so that + * the subsequent conversion to Unix-epoch microseconds cannot overflow. + */ + if (ts < UUIDV7_MIN_TIMESTAMP || ts > UUIDV7_MAX_TIMESTAMP) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("timestamp out of range for UUID version 7"), + errdetail("UUID version 7 supports timestamps from 1970-01-01 to approximately year 10889."))); + + /* Convert the TimestampTz value to a Unix-epoch timestamp in usec */ + us = ts + PG_UNIX_EPOCH_OFFSET_US; /* Generate an UUIDv7 */ uuid = generate_uuidv7(us / US_PER_MS, (us % US_PER_MS) * NS_PER_US + ns % NS_PER_US); @@ -752,8 +786,7 @@ uuid_extract_timestamp(PG_FUNCTION_ARGS) + (((uint64) uuid->data[0]) << 40); /* convert ms to us, then adjust */ - ts = (TimestampTz) (tms * US_PER_MS) - - (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC; + ts = (TimestampTz) (tms * US_PER_MS) - PG_UNIX_EPOCH_OFFSET_US; PG_RETURN_TIMESTAMPTZ(ts); } diff --git a/src/test/regress/expected/uuid.out b/src/test/regress/expected/uuid.out index 95392003b86..5212eb992bf 100644 --- a/src/test/regress/expected/uuid.out +++ b/src/test/regress/expected/uuid.out @@ -248,6 +248,28 @@ SELECT y, ts, prev_ts FROM uuidts WHERE ts < prev_ts; ---+----+--------- (0 rows) +-- uuidv7: infinite intervals are rejected +SELECT uuidv7('infinity'::interval); +ERROR: interval out of range for UUID version 7 +DETAIL: UUID version 7 does not support infinite intervals. +SELECT uuidv7('-infinity'::interval); +ERROR: interval out of range for UUID version 7 +DETAIL: UUID version 7 does not support infinite intervals. +-- uuidv7: timestamps before Unix epoch are rejected +SELECT uuidv7('-1000 years'::interval); +ERROR: timestamp out of range for UUID version 7 +DETAIL: UUID version 7 supports timestamps from 1970-01-01 to approximately year 10889. +-- uuidv7: timestamps beyond 48-bit ms field (~year 10889) are rejected +SELECT uuidv7('9000 years'::interval); +ERROR: timestamp out of range for UUID version 7 +DETAIL: UUID version 7 supports timestamps from 1970-01-01 to approximately year 10889. +-- uuidv7: a large but in-range forward shift is accepted +SELECT uuid_extract_timestamp(uuidv7('1000 years'::interval)) > now() + '999 years'::interval; + ?column? +---------- + t +(1 row) + -- extract functions -- version SELECT uuid_extract_version('11111111-1111-5111-8111-111111111111'); -- 5 diff --git a/src/test/regress/sql/uuid.sql b/src/test/regress/sql/uuid.sql index 465153a0341..e92dd2f2ead 100644 --- a/src/test/regress/sql/uuid.sql +++ b/src/test/regress/sql/uuid.sql @@ -131,6 +131,19 @@ WITH uuidts AS ( ) SELECT y, ts, prev_ts FROM uuidts WHERE ts < prev_ts; +-- uuidv7: infinite intervals are rejected +SELECT uuidv7('infinity'::interval); +SELECT uuidv7('-infinity'::interval); + +-- uuidv7: timestamps before Unix epoch are rejected +SELECT uuidv7('-1000 years'::interval); + +-- uuidv7: timestamps beyond 48-bit ms field (~year 10889) are rejected +SELECT uuidv7('9000 years'::interval); + +-- uuidv7: a large but in-range forward shift is accepted +SELECT uuid_extract_timestamp(uuidv7('1000 years'::interval)) > now() + '999 years'::interval; + -- extract functions -- version