UUID v4 vs v7 as a Postgres primary key, measured
We inserted 2 million rows with bigint, UUID v4 and two kinds of UUID v7 keys. v7 halved the index work of v4, but only when the ids were truly in order: a common v7 function made the index bigger than v4.
Use UUID v7 instead of v4 for primary keys, but check that your v7 ids are really in creation order. In our test, ordered v7 ids cut the index work of an insert by half compared with v4 and kept the index as tightly packed as a bigint one. A popular way of making v7 in plain SQL, accurate only to the millisecond, did something we didn't expect: it built a bigger index than random v4.
Every number below comes from examples/uuid-v4-vs-v7. bench.sql is the benchmark, run.sh runs it three times, and results.txt is the raw output.
The test
PostgreSQL 16.15 on Ubuntu 24.04, in a Linux container with 4 vCPUs (Intel Xeon at 2.10 GHz), 16 GB of RAM and the default shared_buffers of 128 MB.
For each key type we created a table t (id <key> PRIMARY KEY, payload text) and inserted 2,000,000 rows in batches of 10,000, one transaction per batch, the way an app writes as it goes. The id came from the column default. We tried four keys:
| Key | How it is made |
|---|---|
bigint identity | GENERATED ALWAYS AS IDENTITY, the baseline |
| UUID v4 | gen_random_uuid(): 122 random bits |
| UUID v7 (ms) | Unix time in milliseconds, then random bits, the SQL function you find in most blog posts |
| UUID v7 (sub-ms) | The same, plus 12 bits of the fraction of a millisecond, as RFC 9562 allows and PostgreSQL 18's built-in uuidv7() does |
PostgreSQL 16 has no uuidv7(), so both v7 versions are small SQL functions in bench.sql. SQL functions are slow at this, so we also timed making 2 million ids of each kind with no table. That way, a slow id function doesn't count against the index.
Results
Mean of three runs:
| Key | Insert time | Making the ids | Insert minus ids | Index size | Leaf pages full | WAL written |
|---|---|---|---|---|---|---|
| bigint identity | 3.09 s | 0.21 s | 2.88 s | 42.9 MB | 90.1% | 270 MB |
| UUID v4 | 9.59 s | 1.99 s | 7.60 s | 75.9 MB | 71.4% | 325 MB |
| UUID v7 (ms) | 7.32 s | 3.61 s | 3.71 s | 93.1 MB | 58.4% | 315 MB |
| UUID v7 (sub-ms) | 14.83 s | 11.12 s | 3.71 s | 60.2 MB | 90.0% | 297 MB |
"Leaf pages full" is avg_leaf_density from the pgstattuple extension: how much of each index page holds data. The table itself was 113 MB for every UUID key and 98 MB for bigint, because a UUID takes 16 bytes and a bigint takes 8.
What the numbers say
v4 is the slow one. Once the cost of making the ids is taken out, inserting with random v4 keys took 7.6 seconds against 2.9 for bigint, more than two and a half times as long. Each new random key lands on a random index page, so Postgres keeps splitting pages all over the index and writes more WAL to log them. The index ended up 71% full and 77% larger than the bigint one.
Both v7 versions cut that work in half. Both took 3.7 seconds of insert work, close to bigint. New ids land at the end of the index, where the page is already in memory.
Millisecond-only v7 made the biggest index of all. At 93 MB and 58% full, it was worse than v4. Our generator made about 550 ids per millisecond, and inside one millisecond the order is random: in a separate check, 50,011 of 100,000 ids were smaller than the id made just before them. Postgres's B-tree fills a page to 90% only when it splits the last page of the index. Any other split leaves two half-full pages. With ids arriving slightly out of order, most splits happen just before the end, and the pages left behind are never filled again.
Sub-millisecond v7 behaved like bigint. With 12 more bits of time, none of 100,000 ids came out of order. The index was 90% full, the same as bigint, and only 40% larger, which is the cost of 16-byte keys.
We also timed 10,000 lookups of existing keys in random order. The results ranged from 59 to 91 ms with a lot of run-to-run noise, and all four indexes fit in memory. So this test says nothing about read speed, and we don't claim any difference.
What to do
- On PostgreSQL 18 or later, use the built-in
uuidv7()as the column default. It is written in C and keeps ids in order within a session. - On older Postgres, make the v7 id in your application and insert it, or use a v7 function with sub-millisecond precision like
uuid7_us()in ourbench.sql. Our SQL version is slow to run, so in a busy app it is better to make the id in code. - Before trusting any v7 generator, check the order. The out-of-order query in
bench.sqltakes a second to run. If about half the ids come out smaller than the one before, you have the millisecond-only kind. - If nothing outside the database ever sees the id,
bigintis still smaller and faster. UUIDs pay off when ids are made outside the database, merged from several sources, or shown in URLs where a counter would reveal how many rows you have.
Need a few ids to test with? The UUID generator makes v4, v7, v1, v5 and v3 in your browser, and the other everyday tools are on the developer tools page.
Limits of this test
This was one machine, one session inserting, and 2 million rows that fit in memory. With many sessions inserting at once, or an index bigger than RAM, the v4 penalty usually grows, because random inserts then also mean reading pages from disk. We didn't measure that. Run run.sh on your own hardware, with your own row size, before making a decision that is hard to undo.
Part of the team at Horizech — we build Flutter apps, AI integrations, web platforms, and games. If you want this done for you, get in touch.