Re: UUID v7

From: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
To: Daniel Verite <daniel(at)manitou-mail(dot)org>
Cc: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, Jelte Fennema-Nio <postgres(at)jeltef(dot)nl>, Sergey Prokhorenko <sergeyprokhorenko(at)yahoo(dot)com(dot)au>, Przemysław Sztoch <przemyslaw(at)sztoch(dot)pl>, Michael Paquier <michael(at)paquier(dot)xyz>, Aleksander Alekseev <aleksander(at)timescale(dot)com>, Pgsql-Hackers Mailing List <pgsql-hackers(at)postgresql(dot)org>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Mat Arye <mat(at)timescaledb(dot)com>, Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>, Nikolay Samokhvalov <samokhvalov(at)gmail(dot)com>, Junwang Zhao <zhjwpku(at)gmail(dot)com>, Stepan Neretin <sncfmgg(at)gmail(dot)com>
Subject: Re: UUID v7
Date: 2024-12-17 10:04:47
Message-ID: B82AAB78-C2DE-4EE6-BADB-2D98342EBCDB@yandex-team.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Daniel!

> On 16 Dec 2024, at 19:08, Daniel Verite <daniel(at)manitou-mail(dot)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.

In response to

  • Re: UUID v7 at 2024-12-16 14:08:55 from Daniel Verite

Browse pgsql-hackers by date

  From Date Subject
Next Message Andreas Karlsson 2024-12-17 10:45:52 Re: INSERT ... ON CONFLICT DO SELECT [FOR ...] take 2
Previous Message vignesh C 2024-12-17 09:43:08 Re: Added schema level support for publication.