UUIDv4 Primary Keys and B-Tree Index Performance in PostgreSQL
How random UUIDv4 primary keys affected insert throughput and index size on a large PostgreSQL table, and how UUIDv7 compared in my test.
· 3 min read · Erik Kubica
Why UUIDv4 Became Popular
Somewhere around 2018, a lot of advice started circulating that auto-incrementing integer IDs were a security risk.
"If an attacker sees /orders/1042, they will try /orders/1043!"
The usual fix suggested across many blog posts was to use UUIDv4 for every primary key. It looked modern, it allowed distributed ID generation in Go or Node services without asking the database, and it felt safer.
On a number of projects I've cleaned up databases that followed this advice. When a table has 50,000 rows, UUIDv4 works fine. But once a table reached 15 million rows on a modest server, I saw insert throughput drop sharply.
Random UUIDv4 is a good fit for session tokens and similar uses. As a primary key on a large, write-heavy table, it can get expensive.
How B-Trees Work
A PostgreSQL primary key creates an underlying B-tree index.
B-trees work well with sequential ordering. When you insert monotonic, sequential IDs (like BIGINT GENERATED ALWAYS AS IDENTITY), new entries append to the rightmost leaf node of the B-tree.
The active page stays hot in PostgreSQL's shared_buffers in RAM. Postgres fills the 8 KB page to roughly 90% capacity, flushes it sequentially to disk, and moves to the next page. That needs very little random disk I/O.
Now look at UUIDv4:
a4e25a81-7910-4822-b5b6-c567848f2a91
1f39c099-3174-42b8-933e-e63d410714eb
f928e441-2c11-4770-9831-18d451a9bf1c
Every generated ID is uniformly distributed random data.
When you insert a new row with UUIDv4:
- The key belongs in a random leaf page anywhere across the B-tree.
- If that page is not already in RAM, Postgres must issue a random read from disk into memory.
- If that 8 KB page is full, the database must perform a B-tree page split: allocate a new 8 KB block, move half the keys over, and update the parent node pointers.
Throughput in My Test
I ran an insert test on a 4-core database server (4 vCPU, 8 GB RAM, NVMe storage), inserting 20 million rows into Postgres 16.
This is the throughput once the table and index outgrew the 4 GB shared_buffers setting:
| Metric | Sequential BIGINT | UUIDv4 (Random) | UUIDv7 (Time-Sorted) |
|---|---|---|---|
| Inserts / sec (at 1M rows) | 24,800 req/s | 23,100 req/s | 24,200 req/s |
| Inserts / sec (at 15M rows) | 21,500 req/s | 3,150 req/s | 19,800 req/s |
| Primary Key Index Size | 420 MB | 1,840 MB | 710 MB |
| Average Page Fill Factor | 91% | 58% | 89% |
| Disk Write Amplification | 1.1x | 4.8x | 1.2x |
In this setup, UUIDv4 insert speed dropped by about 86%. The primary key index grew to 1.84 GB because page splits left pages partly empty, and a large share of the time went to waiting on random disk reads for index pages that weren't cached. Your results will depend on your hardware, table size and memory.
An Alternative: UUIDv7
If you need client-generated IDs that are not guessable and don't expose sequential numbers to users, UUIDv7 is worth considering as an alternative to UUIDv4. If neither requirement applies, a plain sequential BIGINT is the simplest choice and was the fastest in my test.
UUIDv7 combines a 48-bit UNIX millisecond timestamp at the beginning with 74 bits of random data at the end:
-- Generating UUIDv7 natively in PostgreSQL
CREATE TABLE orders (
id uuid DEFAULT uuid_generate_v7() PRIMARY KEY,
tenant_id text NOT NULL,
amount_cents bigint NOT NULL,
created_at timestamptz DEFAULT now()
);
Because the leading 48 bits increase with time, inserts land on the hot end of the B-tree. You keep the distributed generation benefits of UUIDs and get index locality much closer to sequential integers.
If you already use UUIDs for primary keys on a table that will get large, it may be worth checking whether UUIDv7 fits your case.