UUIDv7 – The Index-Friendly Choice
Mar 16, 2026
UUIDv7 – The Index-Friendly Choice: Why Switching From Random UUIDs to Timestamp-Ordered IDs is the Best Schema Change for 2026
If you are running a distributed system, you likely abandoned auto-incrementing integer IDs a long time ago. They leak business intelligence, complicate database sharding, and create absolute chaos during data merges.
The industry standard replacement has been UUIDv4. It gives you a 128-bit virtually collision-proof identifier. But as your tables grow from millions to billions of rows, a silent performance killer starts suffocating your database: index fragmentation.
Here is why swapping your random UUIDs for timestamp-ordered UUIDv7 is the single highest-ROI schema change you can make for your PostgreSQL database right now.
The Problem With UUIDv4: A B-Tree Nightmare
PostgreSQL handles primary keys using B-Tree indexes. When you insert a new row, Postgres has to place that new ID into the index in its correct sorted position.
UUIDv4 is entirely random. This means every new insert requires Postgres to write to a random page in the B-Tree. At scale, this creates a domino effect of performance degradation:
Cache Thrashing: Because inserts are scattered, Postgres cannot keep the "hot" index pages in memory (RAM). It constantly has to evict pages and fetch old ones from the disk, drastically increasing I/O operations.
Page Splits and Bloat: Inserting random data into the middle of full B-Tree pages forces Postgres to split those pages, leaving them partially empty. This balloons the physical size of your index.
WAL Bloat: Every page split generates significant Write-Ahead Log (WAL) traffic, increasing replication lag and disk write saturation.
When you are actively hunting down database bottlenecks and monitoring system health, massive index bloat from UUIDv4 is often the prime suspect behind degrading write throughput.
Enter UUIDv7: The Best of Both Worlds
RFC 9562 officially standardized UUIDv7, and it elegantly solves the fragmentation problem while keeping all the benefits of a universally unique identifier.
Instead of being 100% random, UUIDv7 is time-ordered. It encodes a Unix timestamp with millisecond precision into the first 48 bits of the UUID, followed by random data for the remaining bits.
UUIDv4 vs. UUIDv7 at a Glance
Feature | UUIDv4 | UUIDv7 |
Generation Method | Purely Random | Timestamp-First + Random |
Sortable? | No | Yes (Chronologically) |
B-Tree Insert Pattern | Random (High I/O) | Sequential (Low I/O) |
Index Bloat | High | Minimal |
Collision Resistance | Extremely High | Extremely High |
Why UUIDv7 is a Game-Changer for PostgreSQL
By placing the timestamp at the beginning of the string, UUIDv7 ensures that newly generated IDs are strictly greater than previously generated ones.
Sequential Writes: New inserts are neatly appended to the right-most edge of your B-Tree index.
Optimized Caching: Postgres only needs to keep the latest index pages in memory. Cache hit rates skyrocket, and disk I/O plummets.
Free Chronological Sorting: Because the ID itself contains the creation timestamp, you can often drop your
created_atindex entirely or use the ID for faster cursor-based pagination.
How to Implement UUIDv7 in Postgres Today
With PostgreSQL 17, native support for generating UUIDv7 is readily available, but you don't need to be on the bleeding edge to start using it.
If you are on an older version of PostgreSQL, you have two great options:
Generate it at the application layer: Most modern web frameworks and languages (like Python) have mature libraries for generating UUIDv7 before the database insert.
Use an extension: Extensions like
pgcryptoor custom C-extensions can handle the generation natively inside yourDEFAULTcolumn definitions.
The Verdict
If you are building SaaS applications where scale and performance are non-negotiable, sticking with UUIDv4 is a self-inflicted wound. Migrating to UUIDv7 stabilizes your B-Trees, drastically reduces disk I/O, and buys your current hardware a significantly longer runway.
For engineers who care deeply about keeping query times in the single-digit milliseconds, standardizing on UUIDv7 is the most obvious win of 2026.