uuidv7 improperly accepts dates before 1970-01-01
uuidv7 improperly accepts dates before 1970-01-01
Hii,
When playing around with UUIDv7s, I discovered that it accepts this:
xof=# SELECT uuidv7(INTERVAL '-1000 years');
uuidv7
--------------------------------------
e4ea52a0-bda1-7121-8f1f-3d9bb3d9a76e
(1 row)
But RFC 9562 defines the time field as an unsigned number of milliseconds since Unix epoch, so timestamps earlier than that should be rejected. "Don't do that" is one answer, but for good hygiene, here's a patch that adds a < 0 check and a regression test. Applies cleanly to HEAD, make check passes.
Re: uuidv7 improperly accepts dates before 1970-01-01
От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
> On 25 Apr 2026, at 05:19, Christophe Pettus wrote: > > "Don't do that" is one answer, but for good hygiene, here's a patch that adds a < 0 check and a regression test. Hi Christophe! We intentionally left ability to overflow unix_ts_ms bits. In some cases one might want to intentionally break time locality by using construction like SELECT uuidv7(INTERVAL '1000 years' * shard_id); This will give time locality for UUIDs generated on each shard. We consulted with RFC authors about this feature, and they confirmed that shifting time is compliant with RFC wording. We wrote the specific test that ensures vast space for shift, but not unlimited. Time shifting would become a footgun if we throw an exception when overflown. If you use SELECT uuidv7(INTERVAL '-1000 years'); for generating identifiers, they will still be unique and time-local, and more over - they will be ascending for a single backend. So no documented guarantees are broken. Thank you! Best regards, Andrey Borodin.