UUIDv7 in PostgreSQL 18: Time-Ordered Primary Keys Without the Index Penalty
Random UUIDs are a popular choice for primary keys because they can be generated anywhere and are hard to guess. They also have a well-known cost: every insert lands at a random spot in the primary key index. PostgreSQL 18 ships a built-in uuidv7() function that keeps the benefits of UUIDs while making new keys sort roughly by creation time. This post explains what that changes inside the database, what the numbers from one public benchmark look like, and where the trade-offs are.
What a UUIDv7 actually is
A UUID is a 128-bit value. Version 4 UUIDs are almost entirely random bits. Version 7 UUIDs put a Unix timestamp in the most significant 48 bits and fill the remainder with random data (plus the usual version and variant bits). Because the timestamp comes first, values generated later compare as larger than values generated earlier.
The PostgreSQL 18 documentation describes uuidv7() as generating a version 7 (time-ordered) UUID, where the timestamp is computed from a UNIX timestamp with millisecond precision, plus a sub-millisecond timestamp, plus random bits. A write-up from Nile adds that the Postgres implementation uses a 12-bit sub-millisecond fraction right after the timestamp, which keeps values generated in the same backend session monotonic.
SELECT uuidv7(); -- a time-ordered UUID, for example 019535d9-3df7-79fb-b466-fa907fa17f9e The leading characters of that example are the timestamp. Two UUIDs created a moment apart share a long common prefix, whereas two version 4 UUIDs share nothing in particular.
Why random keys hurt a B-tree
A primary key in PostgreSQL is backed by a B-tree index. When keys arrive in sorted order, new entries go into the right-most leaf page. That page fills up, a new one is started, and the older pages stay full and untouched. The part of the index you are writing to is small and stays in memory.
With random keys, each new entry belongs somewhere different in the tree. Any leaf page can be the target, so the database has to load it, modify it, and eventually write it back. When a page is full, it splits, which leaves two partly empty pages. Over time this produces a larger, more fragmented index and more cache pressure. The Nile article summarizes the same point: random inserts happen at random locations, which can cause index bloat and other performance problems.
Time-ordered keys fix this by making the insert pattern look like an auto-incrementing integer, while still being globally unique and generatable outside the database.
Using it in a table
Adopting it is a one-line change:
CREATE TABLE orders ( id uuid PRIMARY KEY DEFAULT uuidv7(), customer_id bigint NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); The PostgreSQL 18 docs list uuidv4() and gen_random_uuid() together, both described as generating a version 4 (random) UUID. If you want to see which kind of UUID a column holds, uuid_extract_version(uuid) returns the version number, so it returns 4 for a random UUID and 7 for a time-ordered one.
For existing tables you do not need to rewrite old rows. Changing the column default means new rows get version 7 values while old rows keep their random ones. The index will then contain a mix, and the benefit applies to the new inserts.
Reading the timestamp back
The function uuid_extract_timestamp(uuid) returns a timestamp with time zone for version 1 and version 7 UUIDs, and null for other versions. The documentation notes the extracted value is not necessarily exactly the time the UUID was generated, since that depends on the implementation that created it.
SELECT uuid_extract_timestamp('019535d9-3df7-79fb-b466-fa907fa17f9e'::uuid); -- 2025-02-23 21:46:24.503-05 That is convenient for debugging, but it should not replace a real created_at column. A UUID's embedded time is a side effect of how it was generated, not a data field you control.
The shift parameter
uuidv7() accepts an optional interval that shifts the computed timestamp. According to the docs, infinite intervals are rejected and the shifted timestamp must fall within the range of the 48-bit millisecond field (from 1970-01-01 00:00:00 UTC to roughly year 10889), otherwise an error is raised.
SELECT uuidv7(INTERVAL '-1 day'); A practical use is backfilling or seeding data so that generated keys sort as if the rows had been created at a different time.
What one benchmark showed
The credativ blog ran a comparison on PostgreSQL 18 between tables keyed by uuidv4() and uuidv7(). The author describes it as a single, naive run, and does not describe the hardware, so treat the figures as an illustration of direction rather than a prediction for your system.
Inserting 50 million rows into an empty table took about 20 minutes 40 seconds with UUIDv4 and about 1 minute 46 seconds with UUIDv7. Adding another 50 million rows to a table already holding 50 million took about 46 minutes with UUIDv4 and about 1 minute 40 seconds with UUIDv7. Notably, the version 7 time stayed nearly flat as the table grew, which fits the explanation above: the write-hot part of the index stays small.
Index size also differed. For the 50 million row case the primary key index was 1981 MB with UUIDv4 and 1504 MB with UUIDv7, while the table itself was the same size (2489 MB) in both.
On a 1 million row table, the pgstatindex output showed an average leaf density of 71 for UUIDv4 and about 90 for UUIDv7, with leaf fragmentation of about 50 versus 0. The correlation between key order and physical order was close to zero for UUIDv4 and 1 for UUIDv7.
A caution about that article: the author's prose contains some arithmetic that does not match the tables (for example, the buffer hit ratio and index size percentages are stated loosely), and the query test showed more disk reads for UUIDv7 than UUIDv4, which the article does not discuss. Run a test on your own data and hardware before drawing conclusions.
Trade-offs to weigh
Keys now reveal time. A UUIDv7 contains its creation time, and uuid_extract_timestamp() makes it trivial to read. If you expose IDs in URLs or APIs, a client can infer roughly when a record was created and, by sampling, how fast you create them. Random UUIDs do not leak this. If creation time is sensitive, keep a separate random public identifier.
Ordering is only approximate across machines. Within one backend session, the sub-millisecond bits keep values monotonic, per the Nile article. Across multiple application servers or databases, clocks differ, so values are roughly but not strictly ordered. That is still fine for index locality, but do not use the UUID as a strict sequence number or as a source of truth for event ordering.
Size. A UUID is 128 bits, larger than an integer or bigint. For tables with many tiny rows, or when every foreign key and secondary index repeats the key, that overhead adds up. Nile recommends using the binary uuid type in both the database and application rather than storing the string form, which is both smaller and faster to compare.
Existing data stays random. A table with years of version 4 keys will keep its fragmented index until it is rebuilt. New writes improve, old pages do not.
When it is a good fit
UUIDv7 is a strong default when you need identifiers generated by clients or multiple services, want to avoid a central sequence, and still care about insert throughput on large tables. It is less compelling for small tables, where index locality hardly matters, or where the key must be unguessable and carry no timing information.
A reasonable way to adopt it is to change the default on one high-write table, watch index size and insert latency over a few weeks, and compare against your baseline before rolling it out more widely.