Postgres uuidv7 in PostgreSQL 18: Time-Ordered Primary Keys in Ten Minutes

PostgreSQL 18 ships a built-in uuidv7() function, so time-ordered UUID primary keys need no extension. A ten-minute lab: make a table, read the timestamp back out, backfill with a shift, and know when not to use them.

Ada Brackett

5 min read

By Ada Brackett — Solnyxus editorial voice.

Picture your primary key index as a bookshelf. Every time you insert a row with a random UUID, someone walks over and jams a new book into a random spot in the middle. Shelves split. Books shuffle. Do that a few million times and the whole thing is half empty and wildly out of order. Postgres uuidv7 is the fix, and here's the part that made me grin: since PostgreSQL 18 it's built right in. No extension. Nothing to install.

So let's play with it. This is a ten-minute lab, and I checked everything in it on 7 November 2026 against the PostgreSQL 18 documentation.

What a version 7 UUID actually is

UUID version 7 comes from RFC 9562, published in May 2024. The first 48 bits are a Unix timestamp in milliseconds. Most of what's left is random.

That ordering is the whole trick. Time comes first, so a new value sorts after an old one, and a B-tree index can just add new keys near its right-hand edge instead of splitting pages all over the tree. The shelf fills from left to right, like a sensible shelf.

PostgreSQL 18 gives you two functions. uuidv7() returns a fresh version 7 UUID, and it takes an optional interval that shifts the timestamp (we'll use that in a minute). uuidv4() is a new name for gen_random_uuid(), which still works fine if you've got it all over your schema.

One more nice detail: Postgres 18 fills some of the bits after the millisecond with a finer-grained timestamp, so values you make one after another in the same session come out in order.

Start a Postgres 18 sandbox

Got Docker? Then you've got a throwaway server in two lines:

Bash
docker run --rm -d --name pg18 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:18
docker exec -it pg18 psql -U postgres

The first line starts PostgreSQL 18 with no password; the second drops you into psql inside it. (Side note: no password is fine for a lab on your own laptop. Never for anything real. I'm saying it twice because someone always copies the lab line into production.)

Postgres uuidv7 as a default key

SQL
CREATE TABLE notes (
    id         uuid PRIMARY KEY DEFAULT uuidv7(),
    body       text NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now()
);

INSERT INTO notes (body) VALUES ('first'), ('second'), ('third')
RETURNING id;

The default does all the work: any insert that leaves id out gets a brand-new version 7 UUID.

Now look closely at those three ids. They start with the same characters, because they were born in the same moment, and only drift apart further along. That shared prefix is the timestamp staring back at you.

Read the time back out

SQL
SELECT id,
       uuid_extract_version(id)   AS version,
       uuid_extract_timestamp(id) AS made_at
FROM notes
ORDER BY id;

uuid_extract_version says 7. uuid_extract_timestamp hands back the millisecond the key was made, as a timestamptz. And ordering by id gives you the insert order. I love this. The key tells you its own birthday.

Backfill old rows with a shift

Say you're importing rows from an older system and you want their keys to sort by their real age, not by the day you ran the import. Pass an interval:

SQL
INSERT INTO notes (id, body, created_at)
VALUES (uuidv7(interval '-30 days'), 'imported', now() - interval '30 days');

SELECT body, uuid_extract_timestamp(id) FROM notes ORDER BY id;

The imported row jumps to the front. Its timestamp sits 30 days in the past, so that's where it sorts.

See the index difference for yourself

Don't take my word for any of this. Build two tables, one keyed with uuidv4() and one with uuidv7(), and pour a million rows into each:

SQL
CREATE TABLE k4 (id uuid PRIMARY KEY DEFAULT uuidv4(), n int);
CREATE TABLE k7 (id uuid PRIMARY KEY DEFAULT uuidv7(), n int);

\timing on
INSERT INTO k4 (n) SELECT g FROM generate_series(1, 1000000) g;
INSERT INTO k7 (n) SELECT g FROM generate_series(1, 1000000) g;

SELECT pg_size_pretty(pg_relation_size('k4_pkey')) AS v4_index,
       pg_size_pretty(pg_relation_size('k7_pkey')) AS v7_index;

\timing on makes psql print how long each insert took, and the last query puts the two primary key indexes side by side. Random keys split pages everywhere. Time-ordered keys fill pages in order. Run it and compare the numbers on your own machine; watching them is half the fun.

My honest opinion? For a new table where you want a UUID key, I'd reach for uuidv7() first and make random keys justify themselves.

When not to use it

Here's the catch, and it's a real one. A version 7 UUID carries its creation time, and anyone who sees the id can read it. If the moment a row was created is sensitive (when an account signed up, when an order was placed), keep these ids away from the public. Give the outside world a separate random public id instead.

And ordering is only reliable within one session. Two sessions inserting in the same millisecond can hand out keys out of order. When the exact order matters, keep a real created_at column. That's why the table above has one.

Try this next

Grab a test copy of one of your own tables, switch it from gen_random_uuid() to uuidv7(), and run the index-size query before and after a bulk load. Then, just for fun, point uuid_extract_timestamp at an old version 4 key. You'll get NULL back. A version 4 UUID has no time in it at all, and now you know exactly why.