Reject infinite and out-of-range interval shifts in uuidv7().
uuidv7(interval) shifts the current time by the given interval before
encoding it into the 48-bit Unix-millisecond timestamp field of the
generated UUID. Two cases were mishandled:
An infinite interval ('infinity' or '-infinity') produced an infinite
timestamp, which overflowed during the conversion to Unix-epoch
microseconds and yielded a garbage UUID. Reject infinite intervals up
front, before any timestamp arithmetic.
A shift that moved the timestamp outside the range representable by
the 48-bit field was silently accepted. Timestamps before the Unix
epoch wrapped when cast to unsigned, and timestamps beyond
approximately year 10889 overflowed the field; both produced UUIDs
with bogus timestamps that break sort ordering. Reject any shifted
timestamp outside the supported range.
Also document that infinite intervals and out-of-range shifts are
rejected.
Although raising a new error changes behavior in a stable branch, this
is back-patched to 18 (where uuidv7(interval) was introduced) because
the previous behavior can silently corrupt data. Failing loudly is far
safer than silently accepting the wraparound; otherwise users may not
discover that their UUIDv7 values are no longer sortable until years
later, when recovery is painful. It also matches how PostgreSQL
already handles timestamp + interval overflow, which raises an
error. The change only affects applications passing an interval large
enough to push the result outside the representable range.
Backpatch to 18, where uuidv7(interval) was introduced.
Reported-by: Christophe Pettus <[email protected]>
Author: Baji Shaik <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Reviewed-by: Zsolt Parragi <[email protected]>
Reviewed-by: Tristan Partin <[email protected]>
Reviewed-by: Kyotaro Horiguchi <[email protected]>
Discussion:
https://www.postgresql.org/message-id/[email protected]
Backpatch-through: 18
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/7575215e0b9cf56340ea5fc202fa45f7613852c9
Modified Files
--------------
doc/src/sgml/func/func-uuid.sgml | 6 +++++
src/backend/utils/adt/uuid.c | 45 +++++++++++++++++++++++++++++++++-----
src/test/regress/expected/uuid.out | 22 +++++++++++++++++++
src/test/regress/sql/uuid.sql | 13 +++++++++++
4 files changed, 80 insertions(+), 6 deletions(-)