<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vaibhav Gupta</title>
    <description>The latest articles on DEV Community by Vaibhav Gupta (@vaibhav_taprico).</description>
    <link>https://dev.to/vaibhav_taprico</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4248854%2F0a3cd2e3-59a8-4f91-ae31-9ae1d0b1a66b.png</url>
      <title>DEV Community: Vaibhav Gupta</title>
      <link>https://dev.to/vaibhav_taprico</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vaibhav_taprico"/>
    <language>en</language>
    <item>
      <title>Why Random UUID v4 Primary Keys Hurt Postgres Write Latency (And How RFC 9562 UUID v7 Fixes It)</title>
      <dc:creator>Vaibhav Gupta</dc:creator>
      <pubDate>Tue, 17 Nov 2026 04:49:06 +0000</pubDate>
      <link>https://dev.to/vaibhav_taprico/why-random-uuid-v4-primary-keys-hurt-postgres-write-latency-and-how-rfc-9562-uuid-v7-fixes-it-285e</link>
      <guid>https://dev.to/vaibhav_taprico/why-random-uuid-v4-primary-keys-hurt-postgres-write-latency-and-how-rfc-9562-uuid-v7-fixes-it-285e</guid>
      <description>&lt;p&gt;If you use &lt;code&gt;gen_random_uuid()&lt;/code&gt; (UUID v4) as the primary key across high-write PostgreSQL tables, you get zero coordination overhead and unguessable public IDs—but once a table outgrows &lt;code&gt;shared_buffers&lt;/code&gt;, random primary keys quietly destroy your write throughput.&lt;/p&gt;

&lt;p&gt;Here is what actually happens inside the B-Tree index, how &lt;strong&gt;RFC 9562 UUID v7&lt;/strong&gt; solves it without changing your schema type, and a practical workflow to test UUID v7 data locally before migrating.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Hidden Cost of Random UUID v4 in B-Tree Indexes
&lt;/h2&gt;

&lt;p&gt;A standard UUID v4 places &lt;strong&gt;122 pseudo-random bits&lt;/strong&gt; across a 128-bit integer. Because PostgreSQL indexes &lt;code&gt;uuid&lt;/code&gt; primary keys in a standard B-Tree:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Every &lt;code&gt;INSERT&lt;/code&gt; lands on a &lt;strong&gt;completely random leaf page&lt;/strong&gt; inside the index instead of appending sequentially at the rightmost edge.&lt;/li&gt;
&lt;li&gt;Once the index exceeds RAM (&lt;code&gt;shared_buffers&lt;/code&gt;), PostgreSQL constantly evicts hot pages and pulls cold leaf pages back from disk.&lt;/li&gt;
&lt;li&gt;Half-full pages split repeatedly (&lt;strong&gt;B-Tree page splits&lt;/strong&gt;), inflating index size on disk and multiplying &lt;strong&gt;Write-Ahead Log (WAL)&lt;/strong&gt; volume during replication.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Compare that with sequential keys (&lt;code&gt;BIGINT&lt;/code&gt; or time-ordered UUIDs):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric (High-Churn Table)&lt;/th&gt;
&lt;th&gt;Random UUID v4&lt;/th&gt;
&lt;th&gt;Time-Ordered UUID v7&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Insert Locality&lt;/td&gt;
&lt;td&gt;Random across entire B-Tree&lt;/td&gt;
&lt;td&gt;Append-mostly (rightmost leaf pages)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Buffer Cache Hit Ratio&lt;/td&gt;
&lt;td&gt;Degrades rapidly past RAM size&lt;/td&gt;
&lt;td&gt;Stays high on recent writes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Index Bloat &amp;amp; Page Splits&lt;/td&gt;
&lt;td&gt;High under concurrent inserts&lt;/td&gt;
&lt;td&gt;Low, predictable fill factor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lexicographical Time Sort&lt;/td&gt;
&lt;td&gt;Impossible&lt;/td&gt;
&lt;td&gt;Built-in (millisecond precision)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Postgres Column Type&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;uuid&lt;/code&gt; (16 bytes)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;uuid&lt;/code&gt; (16 bytes — drop-in compatible)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  2. Inside RFC 9562 UUID v7
&lt;/h2&gt;

&lt;p&gt;Standardized in &lt;strong&gt;RFC 9562&lt;/strong&gt;, UUID v7 preserves the familiar &lt;code&gt;8-4-4-4-12&lt;/code&gt; hex layout (&lt;code&gt;019339a4-7b20-7c1e-9a42-3f8d12c9b410&lt;/code&gt;) while structuring the first 48 bits as a &lt;strong&gt;big-endian Unix timestamp in milliseconds&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           unix_ts_ms (32 bits)                |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          unix_ts_ms (16 bits) |  ver  |       rand_a (12b)    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var|                       rand_b (62 bits)                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           rand_b (continued)                  |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;unix_ts_ms&lt;/code&gt; sits in the &lt;strong&gt;most significant bits&lt;/strong&gt;, PostgreSQL sorts new rows in chronological order automatically—without exposing predictable sequential integer counts like &lt;code&gt;/orders/1042&lt;/code&gt; to external users.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Using UUID v7 in SQL &amp;amp; Application Code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  PostgreSQL 18+ (Native &lt;code&gt;uuidv7()&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;PostgreSQL 18 introduced native &lt;code&gt;uuidv7()&lt;/code&gt; directly in core:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nf"&gt;uuidv7&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Node.js / TypeScript (Or Quick Local Test Batches)
&lt;/h3&gt;

&lt;p&gt;If you are on PostgreSQL 15–17 or generating IDs at the application edge (Node.js, Cloudflare Workers, Next.js), you can generate RFC 9562 UUID v7 strings before executing parameterized &lt;code&gt;INSERT&lt;/code&gt;s—or quickly generate bulk SQL &lt;code&gt;INSERT&lt;/code&gt; seed fixtures locally using a browser-side tool like the &lt;a href="https://taprico.com/tools/uuid-generator/" rel="noopener noreferrer"&gt;Taprico UUID v4 &amp;amp; UUID v7 Generator&lt;/a&gt; (runs 100% client-side and exports SQL &lt;code&gt;INSERT&lt;/code&gt;, JSON, or CSV batches).&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Two Practical Migration Rules Before Switching
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keep Existing UUID v4 Rows Untouched:&lt;/strong&gt;
Because UUID v4 and UUID v7 share the exact same 128-bit &lt;code&gt;uuid&lt;/code&gt; PostgreSQL column type, you do &lt;strong&gt;not&lt;/strong&gt; need to rewrite historical primary keys. Simply switch the column &lt;code&gt;DEFAULT&lt;/code&gt; (or app generator) for new writes—existing foreign keys keep working cleanly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch Clock Skew in High-Throughput Monotonic Pools:&lt;/strong&gt;
When generating hundreds of IDs within the same millisecond inside one Node.js worker, ensure your generator preserves monotonic sub-millisecond counters (&lt;code&gt;rand_a&lt;/code&gt;) so batch inserts still land in strict order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to benchmark page splits or read a shorter migration checklist, I also documented the full Postgres B-Tree notes here: &lt;a href="https://taprico.com/guides/postgresql-uuid-v7-vs-uuid-v4-performance/" rel="noopener noreferrer"&gt;PostgreSQL UUID v7 vs. UUID v4 Migration Guide&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Are you already using UUID v7 in production, or still sticking with &lt;code&gt;BIGINT&lt;/code&gt; / ULIDs? Curious to hear what your &lt;code&gt;pg_stat_user_indexes&lt;/code&gt; bloat looks like.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>webdev</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
