pgsql: Fix timestamp overflow in UUIDv7 implementation.

From: Masahiko Sawada <msawada(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix timestamp overflow in UUIDv7 implementation.
Date: 2025-03-28 16:39:34
Message-ID: E1tyCk2-001S6I-1Y@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix timestamp overflow in UUIDv7 implementation.

The uuidv7_interval() function previously converted a shifted
microsecond-precision timestamp (64-bit integer) to another 64-bit
integer representing a timestamp with nanosecond precision. This
conversion caused overflow for dates beyond the year 2262. The
millisecond and sub-millisecond parts were then extracted from this
nanosecond-precision timestamp and stored in UUIDv7 values.

With this commit, the millisecond and sub-millisecond parts are stored
directly into the UUIDv7 value without being converted back to a
nanosecond precision timestamp. Following RFC 9562, the timestamp is
stored as an unsigned integer, enabling support for dates up to the
year 10889.

Reported and fixed by Andrey Borodin, with cosmetic changes and
regression tests by me.

Reported-by: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Author: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Discussion: https://postgr.es/m/96DEC2D9-659A-40E8-B7BA-AF5D162A9E21@yandex-team.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a5419bc72e22337a16655aa28e2e20ecb65f65c3

Modified Files
--------------
src/backend/utils/adt/uuid.c | 34 +++++++++++++++++-----------------
src/test/regress/expected/uuid.out | 15 +++++++++++++++
src/test/regress/sql/uuid.sql | 12 ++++++++++++
3 files changed, 44 insertions(+), 17 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Nathan Bossart 2025-03-28 17:29:01 pgsql: Rename TRY_POPCNT_FAST to TRY_POPCNT_X86_64.
Previous Message Peter Eisentraut 2025-03-28 15:28:35 pgsql: Tidy up locale thread safety in ECPG library.