Re: UUID v7

x4mmm@yandex-team.ru

From: Andrey Borodin <x4mmm@yandex-team.ru>
To: Daniel Verite <daniel@manitou-mail.org>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>, 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: 2024-12-17T10:04:47Z
Lists: pgsql-hackers
Hi Daniel!

> On 16 Dec 2024, at 19:08, Daniel Verite <daniel@manitou-mail.org> wrote:
> 
> The timestamps are now just a sequence incrementing by 1
> on each call, independently of the server's clock and
> the actual time span between calls. It has become a counter
> and will remain so until the backend terminates.

This is exactly what RFC suggest us to do. It’s a feature, not a bug.

> 
> It does not have to be that way. In get_real_time_ns_ascending(),
> it could switch immediately to the new time:
> 
> diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c
> index 2e32592f57..8df194daea 100644
> --- a/src/backend/utils/adt/uuid.c
> +++ b/src/backend/utils/adt/uuid.c
> @@ -505,8 +505,11 @@ get_real_time_ns_ascending()
> ns = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
> #endif
> 
> - /* Guarantee the minimal step advancement of the timestamp */
> - if (previous_ns + SUBMS_MINIMAL_STEP_NS >= ns)
> + /*
> + * Guarantee the minimal step advancement of the timestamp,
> + * unless the clock has moved backward.
> + */
> + if (previous_ns + SUBMS_MINIMAL_STEP_NS >= ns && previous_ns <= ns)
> ns = previous_ns + SUBMS_MINIMAL_STEP_NS;
> previous_ns = ns;

We have that previous_ns to protect us from clocks moving backwards. And you suggest us to disable this protection.
To achieve this we would rather delete previous_ns at all. It was there not to guarantee minimal step, but to ensure clocks always move forward only.

> 
>> Also PFA a prototype of making uuidv7() ordered across all backends via
>> keeping previous_ns in shared memory. IMO it's overcomplicating and RFC
>> does not require such guarantees
> 
> It does not have to be in core, but an extension might want to provide
> a generator that guarantees monotonicity across backends.

AFAIK extension pg_uuidv7 does not have this protection right now. But Florian might add it in future.


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