Taprico
Engineering & DevOps 5 min read · Updated November 2026

PostgreSQL UUID v7 vs. UUID v4: B-Tree Locality, Index Bloat & Migration Tips

Keep globally unique distributed primary keys without destroying database buffer cache hit ratios.

Why Random UUID v4 Hurts High-Write Relational Tables

Standard UUID v4 places 122 cryptographically random bits across the identifier. While random IDs prevent enumeration and eliminate coordination across microservices, inserting random keys into a clustered or B-Tree index scatters writes across random memory pages.

Once a high-churn Postgres table outgrows shared_buffers, random UUID v4 inserts trigger constant page splits, extra WAL write amplification, and degraded cache locality compared to monotonic IDs.

How RFC 9562 UUID v7 Solves Index Fragmentation

RFC 9562 UUID v7 begins with a 48-bit big-endian Unix timestamp in milliseconds followed by sub-millisecond monotonic/random entropy:

  • New inserts append sequentially to the rightmost B-Tree leaf pages—dramatically lowering IOPS and index bloat.
  • Retains the exact 128-bit UUID wire format (8-4-4-4-12 hex string), requiring zero schema type changes in PostgreSQL uuid columns.
  • Natural lexicographical sorting by creation time improves cursor pagination and multi-region ETL watermarks.

Generate RFC 9562 UUID v7 & v4 Batches Locally

Create, inspect timestamp bits, or export UUID v7/v4 test rows as SQL INSERTs, JSON, or CSV.