<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>性能 on Last DBA</title><link>https://lastdba.com/en/tags/%E6%80%A7%E8%83%BD/</link><description>Recent content in 性能 on Last DBA</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><copyright>© 2026 liuzhilong62</copyright><lastBuildDate>Fri, 29 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://lastdba.com/en/tags/%E6%80%A7%E8%83%BD/index.xml" rel="self" type="application/rss+xml"/><item><title>UUID v4 and v7: Collision Incidents and Performance Benchmarks</title><link>https://lastdba.com/en/2026/05/29/uuid-v4-and-v7-collision-incidents-and-performance-benchmarks/</link><pubDate>Fri, 29 May 2026 00:00:00 +0000</pubDate><guid>https://lastdba.com/en/2026/05/29/uuid-v4-and-v7-collision-incidents-and-performance-benchmarks/</guid><description>&lt;blockquote&gt;&lt;p&gt;Source material: &lt;a href="https://news.ycombinator.com/item?id=48060054" target="_blank" rel="noreferrer"&gt;HN UUID v4 Collision Thread&lt;/a&gt;, &lt;a href="https://dev.to/umangsinha12/postgresql-uuid-performance-benchmarking-random-v4-and-time-based-v7-uuids-n9b" target="_blank" rel="noreferrer"&gt;dev.to UUID Benchmark&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;blockquote&gt;&lt;p&gt;AI-generated ratio: 99%&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 class="relative group"&gt;TL;DR
 &lt;div id="tldr" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#tldr" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;UUID v4 collided — someone on HackerNews actually hit a real collision. The root cause was a software stack bug, not math. v4 and v7 have no fundamental difference in collision safety. The real difference is index performance: v7 is time-ordered, B-tree is more compact, writes are 35% faster, indexes are 22% smaller. Your UUID v4 is probably fine, but if you care about index performance, switching to v7 is a cheap win.&lt;/p&gt;</description><content:encoded>&lt;blockquote&gt;&lt;p&gt;Source material: &lt;a href="https://news.ycombinator.com/item?id=48060054" target="_blank" rel="noreferrer"&gt;HN UUID v4 Collision Thread&lt;/a&gt;, &lt;a href="https://dev.to/umangsinha12/postgresql-uuid-performance-benchmarking-random-v4-and-time-based-v7-uuids-n9b" target="_blank" rel="noreferrer"&gt;dev.to UUID Benchmark&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;blockquote&gt;&lt;p&gt;AI-generated ratio: 99%&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 class="relative group"&gt;TL;DR
 &lt;div id="tldr" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#tldr" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;UUID v4 collided — someone on HackerNews actually hit a real collision. The root cause was a software stack bug, not math. v4 and v7 have no fundamental difference in collision safety. The real difference is index performance: v7 is time-ordered, B-tree is more compact, writes are 35% faster, indexes are 22% smaller. Your UUID v4 is probably fine, but if you care about index performance, switching to v7 is a cheap win.&lt;/p&gt;

&lt;h3 class="relative group"&gt;The UUID v4 Collision Incident
 &lt;div id="the-uuid-v4-collision-incident" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-uuid-v4-collision-incident" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;A HackerNews thread blew up — &lt;a href="https://news.ycombinator.com/item?id=48060054" target="_blank" rel="noreferrer"&gt;Ask HN: We just had an actual UUID v4 collision&amp;hellip;&lt;/a&gt;, 479 upvotes, 347 comments.&lt;/p&gt;
&lt;p&gt;The OP&amp;rsquo;s own words:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I know what you&amp;rsquo;re thinking&amp;hellip; and I still can&amp;rsquo;t believe it, but&amp;hellip; This morning, our database flagged a duplicate UUID (v4).&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;It wasn&amp;rsquo;t a double-insert bug. The code didn&amp;rsquo;t write it twice. Only ~15,000 rows in the table, using npm&amp;rsquo;s &lt;code&gt;uuid&lt;/code&gt; package &lt;code&gt;uuidv4()&lt;/code&gt;, and two rows created at different times collided on the same UUID:&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;b6133fd6-70fe-4fe3-bed6-8ca8fc9386cd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;What&amp;rsquo;s the probability of a UUID v4 collision? 122 random bits, 2^122 ≈ 5.3×10^36 possibilities. With 15,000 records, collision probability is roughly 2×10^-29. Theoretically &amp;ldquo;impossible.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;But it happened.&lt;/p&gt;

&lt;h4 class="relative group"&gt;Cause 1: Unreliable entropy sources
 &lt;div id="cause-1-unreliable-entropy-sources" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#cause-1-unreliable-entropy-sources" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;p&gt;HN&amp;rsquo;s top-voted comment (jandrewrogers):&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;UUIDv4 security depends on high-quality entropy sources. Hardware defects, software bugs, and misunderstandings of &amp;ldquo;high-quality entropy&amp;rdquo; all break this assumption. Detecting entropy source failures is expensive, so nobody checks — until a collision happens.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;UUID v4 is &lt;strong&gt;explicitly banned&lt;/strong&gt; in high-reliability systems because entropy source quality cannot be verified.&lt;/p&gt;

&lt;h4 class="relative group"&gt;Cause 2: Known npm uuid package bugs
 &lt;div id="cause-2-known-npm-uuid-package-bugs" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#cause-2-known-npm-uuid-package-bugs" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;p&gt;The npm uuid package README itself warns:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;This module may generate duplicate UUIDs when run in clients with deterministic random number generators, such as Googlebot crawlers.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;More seriously, its internal &lt;code&gt;rng()&lt;/code&gt; function has global mutable state. One commenter pointed out: calling &lt;code&gt;rng()&lt;/code&gt; and sending the result effectively &lt;strong&gt;overwrites someone else&amp;rsquo;s random number, and you can predict it&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Related commit: &lt;a href="https://github.com/uuidjs/uuid/commit/91805f665c38b691ac2cbd" target="_blank" rel="noreferrer"&gt;91805f665c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Community advice: use Node.js built-in &lt;code&gt;crypto.randomUUID()&lt;/code&gt;, not the npm uuid package.&lt;/p&gt;

&lt;h4 class="relative group"&gt;Cause 3: Linux kernel /dev/random race condition
 &lt;div id="cause-3-linux-kernel-devrandom-race-condition" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#cause-3-linux-kernel-devrandom-race-condition" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;p&gt;Another comment:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I encountered duplicate UUIDs during soak testing of a distributed system. After extensive debugging, I found it was a Linux kernel race condition bug — on multi-processor systems, two processes simultaneously reading /dev/random could, with extremely low probability (~one in a million), get the same bytes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4 class="relative group"&gt;Cause 4: Go UUID library not checking return values
 &lt;div id="cause-4-go-uuid-library-not-checking-return-values" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#cause-4-go-uuid-library-not-checking-return-values" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;blockquote&gt;&lt;p&gt;Early Go UUID libraries called random functions without checking the return value length. &amp;ldquo;Request N bytes, got 3 bytes back&amp;rdquo; never happened on most hardware, so nobody checked — until production, where it generated thousands of duplicate UUIDs.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4 class="relative group"&gt;Cause 5: Historical AMD CPU RNG defects
 &lt;div id="cause-5-historical-amd-cpu-rng-defects" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#cause-5-historical-amd-cpu-rng-defects" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;p&gt;Certain AMD CPUs had built-in random number generator issues. VM environments can also &amp;ldquo;virtualize away&amp;rdquo; entropy — both time sources and entropy sources can degrade inside VMs.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;v4 and v7 have no fundamental difference in collision safety. The difference is in the first 48 bits — v4 is random, v7 is a timestamp. You&amp;rsquo;re unlikely to encounter timestamp source issues, and random source issues are equally rare. The HN thread is an interesting edge case. Knowing that a tiny number of people hit it is enough — you don&amp;rsquo;t need to distrust the UUID v4 in your own systems.&lt;/p&gt;
&lt;p&gt;When choosing v4 vs v7, what you should really look at isn&amp;rsquo;t collisions — it&amp;rsquo;s &lt;strong&gt;index performance&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 class="relative group"&gt;UUID v7 Performance Comparison in PG 16
 &lt;div id="uuid-v7-performance-comparison-in-pg-16" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#uuid-v7-performance-comparison-in-pg-16" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;UUID v7 has one concrete advantage over v4 in PostgreSQL: &lt;strong&gt;temporal clustering, more B-tree-friendly&lt;/strong&gt;. v4 can bloat and v7 can bloat too — the difference is simply that v7&amp;rsquo;s first 48 bits are time-ordered, so inserts concentrate on the right side of the B-tree, reducing page splits.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://dev.to/umangsinha12/postgresql-uuid-performance-benchmarking-random-v4-and-time-based-v7-uuids-n9b" target="_blank" rel="noreferrer"&gt;Umang Sinha&amp;rsquo;s benchmark&lt;/a&gt; ran a rigorous comparison on a PG 16 Docker container (8 cores, 16GB, NVMe).&lt;/p&gt;

&lt;h4 class="relative group"&gt;Test Conditions
 &lt;div id="test-conditions" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#test-conditions" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;CREATE&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;TABLE&lt;/span&gt; uuid_v4_test (id UUID &lt;span style="color:#66d9ef"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;KEY&lt;/span&gt;, payload TEXT);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;CREATE&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;TABLE&lt;/span&gt; uuid_v7_test (id UUID &lt;span style="color:#66d9ef"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;KEY&lt;/span&gt;, payload TEXT);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Parameter&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Data volume&lt;/td&gt;
 &lt;td&gt;10 million rows per table&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Batch size&lt;/td&gt;
 &lt;td&gt;10,000 rows per batch&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Client&lt;/td&gt;
 &lt;td&gt;Go + pq driver&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;UUID generation&lt;/td&gt;
 &lt;td&gt;Pre-generated in memory, not timed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 class="relative group"&gt;Performance Results
 &lt;div id="performance-results" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#performance-results" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Metric&lt;/th&gt;
 &lt;th&gt;UUID v4&lt;/th&gt;
 &lt;th&gt;UUID v7&lt;/th&gt;
 &lt;th&gt;Improvement&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Write 10M rows&lt;/td&gt;
 &lt;td&gt;5 min 35 sec&lt;/td&gt;
 &lt;td&gt;3 min 38 sec&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;35% faster&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Table + index total size&lt;/td&gt;
 &lt;td&gt;3618 MB&lt;/td&gt;
 &lt;td&gt;3443 MB&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;5% smaller&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;B-tree index size&lt;/td&gt;
 &lt;td&gt;776 MB&lt;/td&gt;
 &lt;td&gt;602 MB&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;22% smaller&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Point lookup&lt;/td&gt;
 &lt;td&gt;0.167 ms&lt;/td&gt;
 &lt;td&gt;0.038 ms&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;4.4x faster&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Range scan&lt;/td&gt;
 &lt;td&gt;8.283 ms&lt;/td&gt;
 &lt;td&gt;3.791 ms&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;2.2x faster&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 class="relative group"&gt;Why Such a Big Difference
 &lt;div id="why-such-a-big-difference" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#why-such-a-big-difference" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h4&gt;
&lt;p&gt;


&lt;img src="https://lastdba.com/img/uuid-v4-structure.png" alt="UUID v4 bit structure" /&gt;&lt;/p&gt;
&lt;p&gt;


&lt;img src="https://lastdba.com/img/uuid-v7-structure.png" alt="UUID v7 bit structure" /&gt;&lt;/p&gt;
&lt;p&gt;UUID v4 is fully random. Newly inserted UUIDs scatter randomly across the B-tree index, causing massive page splits and severe index fragmentation. UUID v7 has a millisecond-precision timestamp in the first 48 bits, so newly generated UUIDs are naturally ordered — writes cluster on the right side of the B-tree, page splits drop dramatically, and the index is much more compact.&lt;/p&gt;
&lt;p&gt;The 22% smaller index isn&amp;rsquo;t magic — it&amp;rsquo;s &lt;strong&gt;reduced fragmentation&lt;/strong&gt;. Point lookups being 4x faster isn&amp;rsquo;t surprising either — fewer B-tree levels, higher cache hit rates.&lt;/p&gt;

&lt;h3 class="relative group"&gt;Summary
 &lt;div id="summary" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#summary" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;UUID v4 and v7 are identical in collision safety — both depend on entropy source quality, one fills the first 48 bits with random numbers, the other with a timestamp. Collisions are edge cases that a tiny number of people hit in specific environments. Your environment is probably fine — that basic judgment doesn&amp;rsquo;t change.&lt;/p&gt;
&lt;p&gt;What you really should think about is &lt;strong&gt;index performance&lt;/strong&gt;. v7&amp;rsquo;s temporal property makes B-trees more compact, with measured results of 35% faster writes, 22% smaller indexes, and 2-4x faster queries. If your system writes UUIDs at high volume, switching to v7 saves meaningful storage and CPU.&lt;/p&gt;
&lt;p&gt;PG 18 will natively support &lt;code&gt;gen_uuid_v7()&lt;/code&gt;. For now, generate UUIDs at the application layer. Whichever version you use, always add a UNIQUE constraint.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This article was originally published in Chinese on &lt;a href="https://lastdba.com" target="_blank" rel="noreferrer"&gt;lastdba.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;</content:encoded></item></channel></rss>