Re: UUID v7

x4mmm@yandex-team.ru

From: Andrey Borodin <x4mmm@yandex-team.ru>
To: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: Daniel Verite <daniel@manitou-mail.org>, Peter Eisentraut <peter@eisentraut.org>, Jelte Fennema-Nio <postgres@jeltef.nl>, Sergey Prokhorenko <sergeyprokhorenko@yahoo.com.au>, Przemysław Sztoch <przemyslaw@sztoch.pl>, Michael Paquier <michael@paquier.xyz>, Aleksander Alekseev <aleksander@timescale.com>, Pgsql-Hackers Mailing List <pgsql-hackers@postgresql.org>, "David G. Johnston" <david.g.johnston@gmail.com>, Mat Arye <mat@timescaledb.com>, Matthias van de Meent <boekewurm+postgres@gmail.com>, Nikolay Samokhvalov <samokhvalov@gmail.com>, Junwang Zhao <zhjwpku@gmail.com>, Stepan Neretin <sncfmgg@gmail.com>
Date: 2025-01-31T07:08:45Z
Lists: pgsql-hackers

Attachments


> On 31 Jan 2025, at 00:54, Masahiko Sawada <sawada.mshk@gmail.com> wrote:
> 
> I like this idea. Would you like to write a patch, or shall I?

I propose to separate milliseconds from nanoseconds. Please find attached implementation of this.
With this patch we can generate correct UUIDs in a very distant future.
postgres=# select x,                                                                                                                                                uuid_extract_timestamp(uuidv7((x::text || ' year'::text)::interval)),                                                                                        
(x::text || ' year'::text)::interval                                                                                                                  
from generate_series(1,9000,1000) x;
  x   |   uuid_extract_timestamp    |  interval   
------+-----------------------------+------------
    1 | 2026-01-31 12:00:53.084+05  | 1 year
 1001 | 3026-01-31 12:00:53.084+05  | 1001 years
 2001 | 4026-01-31 12:00:53.084+05  | 2001 years
 3001 | 5026-01-31 12:00:53.084+05  | 3001 years
 4001 | 6026-01-31 12:00:53.084+05  | 4001 years
 5001 | 7026-01-31 12:00:53.085+05  | 5001 years
 6001 | 8026-01-31 12:00:53.085+05  | 6001 years
 7001 | 9026-01-31 12:00:53.085+05  | 7001 years
 8001 | 10026-01-31 12:00:53.085+05 | 8001 years
(9 rows)


Best regards, Andrey Borodin.

Commits

  1. Fix timestamp overflow in UUIDv7 implementation.

  2. Add UUID version 7 generation function.

  3. Add some UUID support functions