Re: UUID v7

Masahiko Sawada <sawada.mshk@gmail.com>

From: Masahiko Sawada <sawada.mshk@gmail.com>
To: "Andrey M. Borodin" <x4mmm@yandex-team.ru>
Cc: Sergey Prokhorenko <sergeyprokhorenko@yahoo.com.au>, Jelte Fennema-Nio <postgres@jeltef.nl>, Michael Paquier <michael@paquier.xyz>, Aleksander Alekseev <aleksander@timescale.com>, pgsql-hackers mailing list <pgsql-hackers@postgresql.org>, Peter Eisentraut <peter@eisentraut.org>, Przemysław Sztoch <przemyslaw@sztoch.pl>, "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-11-23T07:58:09Z
Lists: pgsql-hackers

Attachments

On Fri, Nov 22, 2024 at 2:37 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
>
> On Thu, Nov 21, 2024 at 1:22 PM Andrey M. Borodin <x4mmm@yandex-team.ru> wrote:
> >
> >
> >
> > > On 21 Nov 2024, at 02:24, Masahiko Sawada <sawada.mshk@gmail.com> wrote:
> > >
> > >  But does replacing the least significant 2 bits
> > > with random 2 bits really not affect monotonicity?
> >
> > You are right. We have to take into account this when calculating monotonicity. PFA another version.
> >
>
> While it works fine, I think we need a comment for this change:
>
>  -#define SUB_MILLISECOND_STEP ((NS_PER_MS / (1 << 12)) + 1)
>  +#if defined(__darwin__) || _MSC_VER
>  +#define SUB_MILLISECOND_BITS 10
>  +#else
>  +#define SUB_MILLISECOND_BITS 12
>  +#endif
>  +#define SUB_MILLISECOND_STEP ((NS_PER_MS / (1 << SUB_MILLISECOND_BITS)) + 1)
>
> because the reader might think we should use SUB_MILLISECOND_BITS
> here too at a glance:
>
> +       /* sub-millisecond timestamp fraction (12 bits) */
> +       increased_clock_precision = ((ns % NS_PER_MS) * (1 << 12)) / NS_PER_MS;
>

I've attached an updated patch that squashed changes I made for v33.
We're still discussing increasing entropy on Windows and macOS, but
the patch seems to be in good shape.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

Commits

  1. Fix timestamp overflow in UUIDv7 implementation.

  2. Add UUID version 7 generation function.

  3. Add some UUID support functions