From 1be5d674037a5c90b972e06f99d4a4b5a2c3c485 Mon Sep 17 00:00:00 2001 From: Baji Shaik Date: Wed, 24 Jun 2026 18:04:27 -0500 Subject: [PATCH v2 1/3] Reject infinite intervals in uuidv7(interval) uuidv7('infinity'::interval) caused integer overflow during the epoch conversion, producing garbage UUIDs. Reject infinite intervals up front, before any timestamp arithmetic is performed. Also mention in the documentation that infinite interval values are not accepted. Author: Baji Shaik Discussion: https://postgr.es/m/18F007D6-1A33-48C8-BA51-E7A858DE0C89%40thebuild.com --- doc/src/sgml/func/func-uuid.sgml | 1 + src/backend/utils/adt/uuid.c | 7 +++++++ src/test/regress/expected/uuid.out | 7 +++++++ src/test/regress/sql/uuid.sql | 4 ++++ 4 files changed, 19 insertions(+) diff --git a/doc/src/sgml/func/func-uuid.sgml b/doc/src/sgml/func/func-uuid.sgml index 2638e2bf855..cfd42433a95 100644 --- a/doc/src/sgml/func/func-uuid.sgml +++ b/doc/src/sgml/func/func-uuid.sgml @@ -82,6 +82,7 @@ sub-millisecond timestamp + random. The optional parameter shift will shift the computed timestamp by the given interval. + Infinite interval values are not accepted. uuidv7() diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c index 6ee3752ac78..1197cb6c027 100644 --- a/src/backend/utils/adt/uuid.c +++ b/src/backend/utils/adt/uuid.c @@ -672,6 +672,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 diff --git a/src/test/regress/expected/uuid.out b/src/test/regress/expected/uuid.out index 9c5dda9e9ab..28706d77abc 100644 --- a/src/test/regress/expected/uuid.out +++ b/src/test/regress/expected/uuid.out @@ -261,6 +261,13 @@ 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. -- 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 8cc2ad40614..01bbc8558fc 100644 --- a/src/test/regress/sql/uuid.sql +++ b/src/test/regress/sql/uuid.sql @@ -140,6 +140,10 @@ 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); + -- extract functions -- version -- 2.50.1 (Apple Git-155)