Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rtE5U-00DH7b-76 for pgsql-hackers@arkaria.postgresql.org; Sat, 06 Apr 2024 22:00:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rtE5T-00H5B4-8Z for pgsql-hackers@arkaria.postgresql.org; Sat, 06 Apr 2024 22:00:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rtE5R-00H5Av-W2 for pgsql-hackers@lists.postgresql.org; Sat, 06 Apr 2024 22:00:34 +0000 Received: from sonic314-20.consmr.mail.gq1.yahoo.com ([98.137.69.83]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rtE5J-001PnG-HU for pgsql-hackers@postgresql.org; Sat, 06 Apr 2024 22:00:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.au; s=s2048; t=1712440823; bh=klOPs/LMkdaWXC9lX/KL5kHKdVp9mtlsHTlCd5/iljo=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject:Reply-To; b=USJon4e/EvY/y6FpbG/89cGJlBBNpfgGW+uJuvGjeG+03KBCWOm370fjNreiIVXQFJpALudiNxtdXQ6SSreIFp6/LNaKk0r3Pic0bZQfx9/E7py/m68MTLK1d04lGfYTm0esoCIX6nZK48gVgReajKtBRQcxN1Pt6sAyZjWafx3eW7fPdHBXf6c3XCHkaGWq7tJR3c3YXyQcn7ObtPwAjbc3XXNBSmFOIXYkHujjNA39o37t7fv91hTbGmE/M5h4Cs36NzfMVtct9+scAofMUz2m/mt9SLRIDDvM7K54yIRnrs2sJr6zoBYZLI9Dq7lfy5uQG6R5pAlESQDfAzhjgQ== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1712440823; bh=L5ciELePm+iaLQD5/BGEpdB+VEVb4khkD2LWaquiDLp=; h=X-Sonic-MF:Date:From:To:Subject:From:Subject; b=OWA8TimaIsoisUEINBl4uflogNDYsMtKCmgAfvXjjgSUAQGIWcQ9xKB4mVEi7D+x3Gg7IrM3qFxqWiBsfY2OQduvv5KWlfsEZCn2coTRMlKKDSilkhXWXsT+Q4iBA02K2hZFLVk/0kmR0jy1YDbMoy2k12aKvQI7EJo5OmIHPaOYCF1l5eZ1rQA/raAObma8kKy8vUGpfHtoLfbZGojtO6ggMsAVwmRE7wiPDFHS9rVQA08XaF307/Jdg+/qgs6XxebQt6wWBvOHbhOreyMcCs8UQe2KW7YsETHIaa3Go9x7A/3oFHGay6DidcF51k/G7ljS163ulsh8G4LQwvHnpQ== X-YMail-OSG: V1FTWLMVM1nDpn928.9VqxmUsdbkBGruOuGPB7L3M16Ta9kx75jyEPIe7sj4zm9 RoCezUMqAhy5_k6wHP5jLOOi7yY03585mG6NSG7.UTQ77TtjQjdIsG0RyQOSFnCbqEXzeBLknTIX CTFQFEDVm9njfObCgKwULDh2NfBAzqcjs4xYUUlp9R0DNpjjKoAn_v6HrH4HwKs1Nr4TqcCTokaC 39CnsSd..wLkGCcvN6QhB5qKMuDK3Vn9MbbB4MXznTglCrfopnH.SJ3qne31EBN_eO02Eha_uACd .pXgctEwlwy9csGNyqP0_pDZxJsjZj32My8nwDr9XJPrKeHF1FfEsNs4MCl.lOJQdY22pCm5uUKw Sl37MoWzmxdxCUuE429vey3Eg9cpwUjB5q8WW_Fylp0up5LCtQmiKSGqpTzbXcfpkNxIAguWhbyD rj_yta_twa9xy6RQsufAS8VLf..vZKr.8MIByCEZ8ThybV3Us_79w8CLpwXTVFYA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hS74K 1ZmfB7QqlRYSGfjD3XbZzEbOnq0PTFJQk_xv8QMsV1nZxiuax9RQI47IN2t3ohsWFVLCLic5paOl RHjE0dKsAkNi58AlEviDMpMU78KSEhCZerKKrJb..1HleVxvIAmhKXic.r5CmjtOmOtHSIYSsoGI w9BjCa9KaZ5qV8SuBA7EHTadvgCo3RzT7Zycif3XH1YYmFws.phv_hBCj2d1a9VAmLMoBIybiEib VuOq0dKbnAqoMoRvPwyyz8G.tlE3c5BGTaSAal0qw7Amsemv5xlh1Qmcjt8AtgwePoIcOQjkzDh. vpFxDw1OgXi8d0Jm5WrpmuADjR4ehik6zb5dFC2SRtrfuNs2rZ0s9l83eT.XtNmGW3yyQmHgQR_1 cbv7NCKoYANzXajGhxlTiuLcZty_EXcE20ocrLEEN8zq3xX6krR6WZx_GvXSXkm7.0WOLiMP6gzr GGTbAL6MLqp1Fz5swacPWBVFLoCCnq69jid7BvYefSbpbh3N5dDe1t8jaVCONeMXVe4H8pRUWKzw G39.Chiw5ZTf6iA6imKV_AP4guAEMmCpNwFsAdtGVHj63gz1cpAZDWa8X0rkh3AzqIHe0YJvQB2o pSz7KzMEBhHghsBv6EwPA_MtLNQPKe1Zxmb8sWkQ_alXXtj5smq23x6gYgaOvApwUqPXXhXisIro pzCI.sXH2he4AmR0vNip.j9hpQpATYEqtxaFciCXLfmrurYUw04XC.Z1qKyDQtBtrwCDyi9izBsV Z9WhoY9bSoWEIo3tDwDCBzywIRW8quc0zeoikgrc5pA-- X-Sonic-MF: X-Sonic-ID: 52371863-922e-4dab-877a-d99300afb9f7 Received: from sonic.gate.mail.ne1.yahoo.com by sonic314.consmr.mail.gq1.yahoo.com with HTTP; Sat, 6 Apr 2024 22:00:23 +0000 Date: Sat, 6 Apr 2024 21:59:38 +0000 (UTC) From: Sergey Prokhorenko To: Peter Eisentraut , "Andrey M. Borodin" Cc: Aleksander Alekseev , pgsql-hackers mailing list , Jelte Fennema-Nio , =?UTF-8?Q?Przemys=C5=82aw_Sztoch?= , "David G. Johnston" , Mat Arye , Matthias van de Meent , Nikolay Samokhvalov , Junwang Zhao Message-ID: <305478845.5279532.1712440778735@mail.yahoo.com> In-Reply-To: <59729066-7C38-40B9-B8BB-3C04E8BB788D@yandex-team.ru> References: <1971811009.2132347.1706599708209@mail.yahoo.com> <72C1E7CB-E2EC-46CF-AD2C-B3180CED885B@yandex-team.ru> <0906F8BA-CA52-4956-AA68-E9193E50DFDF@yandex-team.ru> <10553e4c-6b66-44b9-86a7-80bb1958a767@eisentraut.org> <0C26E3AC-983E-43A4-B39E-270FF87A0464@yandex-team.ru> <0b1b5e91-177e-4362-8caf-d227d5a1ae3c@eisentraut.org> <2cf9eaf5-057f-4dfa-ae4d-9b23c5339abe@eisentraut.org> <234823B2-D723-4302-8D56-BFE2A62221E8@yandex-team.ru> <7dda76c3-4558-4ad0-a934-09d4751adb4e@eisentraut.org> <59729066-7C38-40B9-B8BB-3C04E8BB788D@yandex-team.ru> Subject: Re: UUID v7 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_5279531_373688775.1712440778734" X-Mailer: WebService/1.1.22205 YMailNorrin Content-Length: 7253 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_Part_5279531_373688775.1712440778734 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable For every complex problem there is an answer that is clear, simple, and wro= ng. Since the RFC allows microsecond timestamp granularity, the first thing= that comes to everyone's mind is to insert microsecond granularity into UU= IDv7. And if the RFC allowed nanosecond timestamp granularity, then they wo= uld try to insert nanosecond granularity into UUIDv7. But I am categorically against abandoning the counter under pressure from t= he unfounded proposal to replace the counter with microsecond granularity. 1) The RFC specifies millisecond timestamp granularity by default. 2) All advanced UUIDv7 implementations include a counter:=E2=80=A2 for Java= Script https://www.npmjs.com/package/uuidv7=E2=80=A2 for Rust https://crate= s.io/crates/uuid7=E2=80=A2 for Go (Golang) https://pkg.go.dev/github.com/go= frs/uuid#NewV7=E2=80=A2 for Python https://github.com/oittaa/uuid6-python 3) The theoretical performance of generating UUIDv7 without loss of monoton= icity for microsecond granularity is only 1000 UUIDv7 per millisecond. This= is very low and insufficient generation performance! But the actual genera= tion performance is even worse, since the generation demand is unevenly dis= tributed within a millisecond. Therefore, a UUIDv7 will not be generated ev= ery microsecond. For a counter 18 bits long, with the most significant bit initialized to ze= ro and the remaining bits initialized to a random number, the actual perfor= mance of generating a UUIDv7 without loss of monotonicity is between 2 to t= he power of 17 =3D 131072 UUIDv7 per millisecond (if the random number happ= ens to be all ones) to 2 to the power of 18 =3D 262144 UUIDv7 per milliseco= nd (if the random number happens to be all zeros). This is more than enough= . 4) Microsecond timestamp fraction subtracts 10 bits from random data, which= increases the risk of collision. In the counter, almost all bits are initi= alized with a random number, which reduces the risk of collision. The only reasonable use of microsecond granularity is when writing to a dat= abase table in parallel. However, monotonicity in this case can be ensured = in another way, namely a single UUIDv7 generator per database table, simila= r to SERIAL (https://postgrespro.com/docs/postgresql/16/datatype-numeric#DA= TATYPE-SERIAL) in PostgreSQL. Best regards, Sergey Prokhorenkosergeyprokhorenko@yahoo.com.au=20 On Thursday, 4 April 2024 at 09:12:17 pm GMT+3, Andrey M. Borodin wrote: =20 =20 =20 ... At this point we can skip the counter\microseconds entirely, just fill ever= ything after unix_ts_ms with randomness. It's still a valid UUIDv7, exhibit= ing much more data locality than UUIDv4. We can adjust this sortability mea= sures later. Best regards, Andrey Borodin. =20 ------=_Part_5279531_373688775.1712440778734 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
For every complex = problem there is an answer that is clear, simple, and wrong. Since the RFC = allows microsecond timestamp granularity, the first thing that comes to eve= ryone's mind is to insert microsecond granularity into UUIDv7. And if the R= FC allowed nanosecond timestamp granularity, then they would try to insert = nanosecond granularity into UUIDv7.

But I am categ= orically against abandoning the counter under pressure from the unfounded p= roposal to replace the counter with microsecond granularity.

=
1) The RFC specifies millisecond timestamp granularity by defaul= t.

2) All advanced UUIDv7 implementations include = a counter:
=E2=80=A2 for JavaScript https://www.npmjs.com/package= /uuidv7
=E2=80=A2 for Rust https://crates.io/crates/uuid7
=E2=80=A2 for Go (Golang) https://pkg.go.dev/github.com/gofrs/uuid#NewV7=
=E2=80=A2 for Python https://github.com/oittaa/uuid6-python

3) The theoretical performance of generating UUIDv7 wi= thout loss of monotonicity for microsecond granularity is only 1000 UUIDv7 = per millisecond. This is very low and insufficient generation performance! = But the actual generation performance is even worse, since the generation d= emand is unevenly distributed within a millisecond. Therefore, a UUIDv7 wil= l not be generated every microsecond.

For a counte= r 18 bits long, with the most significant bit initialized to zero and the r= emaining bits initialized to a random number, the actual performance of gen= erating a UUIDv7 without loss of monotonicity is between 2 to the power of = 17 =3D 131072 UUIDv7 per millisecond (if the random number happens to be al= l ones) to 2 to the power of 18 =3D 262144 UUIDv7 per millisecond (if the r= andom number happens to be all zeros). This is more than enough.
=
4) Microsecond timestamp fraction subtracts 10 bits from ran= dom data, which increases the risk of collision. In the counter, almost all= bits are initialized with a random number, which reduces the risk of colli= sion.



The only reaso= nable use of microsecond granularity is when writing to a database table in= parallel. However, monotonicity in this case can be ensured in another way= , namely a single UUIDv7 generator per database table, similar to SERIAL (h= ttps://postgrespro.com/docs/postgresql/16/datatype-numeric#DATATYPE-SERIAL)= in PostgreSQL.

Best regards,
Se= rgey Prokhorenko
sergeyprokhorenko= @yahoo.com.au


=20
=20
On Thursday, 4 April 2024 at 09:12:17 pm GMT+3, And= rey M. Borodin <x4mmm@yandex-team.ru> wrote:


=20 =20

...


At this point we can skip the counter\microseconds e= ntirely, just fill everything after unix_ts_ms with randomness. It's still = a valid UUIDv7, exhibiting much more data locality than UUIDv4. We can adju= st this sortability measures later.

Best regards, Andrey Borodin.
------=_Part_5279531_373688775.1712440778734--