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 1rjkNO-007aGj-9U for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Mar 2024 18:27:55 +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 1rjkNM-00HCLI-FJ for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Mar 2024 18:27:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rjkNM-00HCLA-0j for pgsql-hackers@lists.postgresql.org; Mon, 11 Mar 2024 18:27:52 +0000 Received: from forwardcorp1b.mail.yandex.net ([178.154.239.136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rjkNJ-003yqn-B1 for pgsql-hackers@postgresql.org; Mon, 11 Mar 2024 18:27:51 +0000 Received: from mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net (mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net [IPv6:2a02:6b8:c08:6c2d:0:640:316f:0]) by forwardcorp1b.mail.yandex.net (Yandex) with ESMTPS id 821DE60CE5; Mon, 11 Mar 2024 21:27:46 +0300 (MSK) Received: from smtpclient.apple (unknown [2a02:6b8:b081:7227::1:3c]) by mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id iRqLlK6IlCg0-BZWiw4fO; Mon, 11 Mar 2024 21:27:45 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1710181665; bh=EwUwkUKIQ97AKI6EHAsqj3pGeZW4K//UvFcehyC0n7c=; h=Message-Id:To:Date:References:Cc:In-Reply-To:From:Subject; b=zouc9SzW8j8jg85FOy4J2OJR7+c1coNv2y//lHCkv/P5s56LjJewNZF77Zv4dmYFN FOwyc8uOFSziv4pdycAwoN5J9sF8t42JXkT/ec8JIadIJBWA02TiP+7Zg1oSVIl8NJ P/blqB2i0ovjaFV79AkkpCcDlLNBd9Il3bPGRd+Q= Authentication-Results: mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.4\)) Subject: Re: UUID v7 From: "Andrey M. Borodin" In-Reply-To: Date: Mon, 11 Mar 2024 23:27:43 +0500 Cc: Aleksander Alekseev , pgsql-hackers mailing list , Peter Eisentraut , Sergey Prokhorenko , =?utf-8?Q?Przemys=C5=82aw_Sztoch?= , "David G. Johnston" , Mat Arye , Matthias van de Meent , Nikolay Samokhvalov , Junwang Zhao Content-Transfer-Encoding: quoted-printable Message-Id: <844E6A65-4A36-4A36-94C2-0CDF7615B53F@yandex-team.ru> References: <0A0D7507-C434-46AC-BCC4-1166C5A2742A@yandex-team.ru> <1459150585.359059.1706132985954@mail.yahoo.com> <00B784AB-5B52-4CC7-8A0D-B1C0C53B52DC@yandex-team.ru> <1014147035.642629.1706202245918@mail.yahoo.com> <5932FFF8-4E5F-44DC-8FEA-22E8302759C7@yandex-team.ru> <8AA22330-7D3A-46CF-B311-2FF874D7C713@yandex-team.ru> <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> <5D22C2BF-5DED-43CA-A3D7-B724EE0A159D@yandex-team.ru> To: Jelte Fennema-Nio X-Mailer: Apple Mail (2.3696.120.41.1.4) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 11 Mar 2024, at 20:56, Jelte Fennema-Nio = wrote: >=20 > Attached a few comment fixes/improvements and a pgindent run (patch = 0002-0004) Thanks! > Now with the added comments, one thing pops out to me: The comments > mention that we use "Monotonic Random", but when I read the spec that > explicitly recommends against using an increment of 1 when using > monotonic random. I feel like if we use an increment of 1, we're > better off going for the "Fixed-Length Dedicated Counter Bits" method > (i.e. change the code to start the counter at 0). See patch 0005 for > an example of that change. >=20 > I'm also wondering if we really want to use the extra rand_b bits for > this. The spec says we MAY, but it does remove the amount of > randomness in our UUIDs. Method 1 is a just a Method 2 with specifically picked constants. But I'll have to use some hand-wavy wordings... UUID consists of these 128 bits: a. Mandatory 2 var and 4 ver bits. b. Flexible but strongly recommended 48 bits unix_ts_ms. These bits = contribute to global sortability of values generated at frequency less = than 1KHz. c. Counter bits: c1. Initialised with 0 on any time tick. c2. Initialised with randomness. c3*. bit width of a counter step (*not counted in 128 bit capacity, can = be non-integral) d. Randomness bits. Method 1 is when c2=3D0. My implementation of method 2 uses c1=3D1, = c2=3D17 Consider all UUIDs generated at any given milliseconds. Probability of a = collision of two UUIDs generated at frequency less than 1KHz is p =3D = 2^-(c2+d) Capacity of a counter has expected value of c =3D 2^(c1)*2^(c2-1)/2^c3 To guess next UUID you can correctly pick one of u =3D 2^(d+c3) First, observe that c3 contributes unguessability at exactly same scale = as decreases counter capacity. There is no difference between using bits = in d directly, or in c3. There is no point in non-zero c3. Every bit = that could be given to c3 can equally be given to d. Second, observe that c2 bits contribute to both collision protection and = counter capacity! And when the time ticks, c2 also contribute to = unguessability! So, technically, we should consider using all available = bits as c2 bits. How many c1 bits do we need? I've chosen one - to prevent occasional = counter capacity reduction. If c1 =3D 1, we can distribute 73 bits between c2 and d. I've chosen c2 = =3D 17 and d =3D 56 as an arbitrary compromise between capacity of one = backend per ms and prevention of global collision. This compromise is mostly dictated by maximum frequency of UUID = generation by one backend, I've chosen 200MHz as a sane value. This compromise is much easier when you do not have 74 spare bits, this = crazy amount of information forgives almost any mistake. Imagine you = have to distribute 10 bits between c2 and d. And you try to prevent = collision between 10 independent devices which need capacity to generate = IDs with frequency of 10KHz each and keep sortability. You would have = something like c1=3D1, c2=3D3,d=3D6. Sorry for this long and vague explanation, if it still seems too = uncertain we can have a chat or something like that. I don't think this = number picking stuff deserve to be commented, because it still is quite = close to random. RFC gives us too much freedom of choice. Thanks! Best regards, Andrey Borodin.=