The primary key type is one of the earliest decisions in a schema and one of the most expensive to reverse. Pick a random UUID and you scatter every insert across the B-tree and pay for it in index bloat for the life of the table. Pick a sequential bigint and you hand anyone who sees the key an estimate of your row count and a centralised generator that has to be online to mint an ID.
UUIDv7, standardised in RFC 9562 (which obsoleted RFC 4122 in May 2024), is the option that resolves most of that tension: a time-ordered UUID that generates well in a distributed fashion and still lands more or less in order at the end of the index. This is a practitioner’s comparison of the three realistic choices for a surrogate key in PostgreSQL — bigint identity, random UUIDv4, and UUIDv7 — on the three things that actually decide the outcome: insert performance, index size, and whether the key is safe to put in a URL.
Why random UUIDs are slow, in one paragraph
A PostgreSQL primary key is backed by a B-tree, and a B-tree stores entries in sorted order. When you insert a monotonically increasing key — a bigint from a sequence, or a time-ordered UUID — every new row lands at the right-hand edge of the tree, so the same few leaf pages stay hot in the buffer cache and page splits are rare. When you insert a random UUIDv4, each key lands in an unpredictable place, so inserts touch leaf pages scattered across the whole index. Those pages have to be read in, dirtied and written back, which multiplies write I/O, fragments the index and inflates its on-disk size. The mechanism is the same one behind the wider trade-offs in how Postgres index types behave under different access patterns. UUIDv7 keeps the time-ordered prefix precisely so that this random scatter does not happen.
The layout is the reason. A UUIDv7 puts a 48-bit Unix-millisecond timestamp in the leading bytes, then the version and variant bits, then randomness. Two IDs generated in the same millisecond still differ, but IDs generated seconds apart sort in creation order. You get the collision-resistance and distributed generation of a UUID with almost the insert locality of a sequence.
Generating each key type in Postgres
One thing to get straight before writing any DDL: the built-in uuidv7() function is a PostgreSQL 18 feature. Version 18 went GA in September 2025 and added uuidv7() plus uuid_extract_timestamp() to core. On PostgreSQL 17 there is no native function — you either install an extension such as pg_uuidv7 (which exposes uuid_generate_v7()) or generate the value in the application and insert it. Do not assume a fresh 17 cluster can call uuidv7(); it cannot.
-- bigint identity: centralised, sequential, 8 bytes
CREATE TABLE t_bigint (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
payload text NOT NULL
);
-- UUIDv4: random, distributed, 16 bytes (gen_random_uuid is core since PG13)
CREATE TABLE t_uuidv4 (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
payload text NOT NULL
);
-- UUIDv7: time-ordered, distributed, 16 bytes (uuidv7() is PostgreSQL 18+)
CREATE TABLE t_uuidv7 (
id uuid DEFAULT uuidv7() PRIMARY KEY,
payload text NOT NULL
);
-- On PostgreSQL 17, replace the v7 default with the extension function:
-- CREATE EXTENSION pg_uuidv7;
-- ... id uuid DEFAULT uuid_generate_v7() PRIMARY KEY ...
Benchmark it yourself — don’t trust a blog’s numbers
The exact figures depend on your hardware, shared_buffers, row width and fill factor, so run the comparison on your own kit rather than trusting anyone’s headline percentage. This harness loads the same five million rows into each table and then reports heap and index size, which is where the difference shows most clearly.
-- Load identical volume into each table
INSERT INTO t_bigint (payload)
SELECT 'row ' || g FROM generate_series(1, 5000000) AS g;
INSERT INTO t_uuidv4 (payload)
SELECT 'row ' || g FROM generate_series(1, 5000000) AS g;
INSERT INTO t_uuidv7 (payload)
SELECT 'row ' || g FROM generate_series(1, 5000000) AS g;
VACUUM ANALYZE t_bigint, t_uuidv4, t_uuidv7;
-- Compare heap vs index footprint
SELECT c.relname AS relation,
pg_size_pretty(pg_table_size(c.oid)) AS heap_size,
pg_size_pretty(pg_indexes_size(c.oid)) AS index_size
FROM pg_class c
WHERE c.relname IN ('t_bigint', 't_uuidv4', 't_uuidv7')
AND c.relkind = 'r'
ORDER BY c.relname;
The direction of the result is reliable even if the magnitude is not. The bigint index is the smallest, because the key is 8 bytes and inserts are perfectly ordered. The UUIDv7 index is larger — a UUID is 16 bytes on disk regardless of version — but stays close to bigint on fragmentation because the time-ordered prefix keeps inserts near the right edge. The UUIDv4 index is the outlier: same 16-byte width, but the random insert pattern leaves it noticeably more bloated and slower to build under sustained write load. To confirm the ordering is really doing its job on PostgreSQL 18, SELECT uuid_extract_timestamp(id) FROM t_uuidv7 ORDER BY id LIMIT 5; should return ascending timestamps.
The bit the benchmark misses: exposure
Performance is only half the decision. The other half is what the key reveals once it leaves the database. A sequential bigint in a URL — /orders/10432 — tells any observer roughly how many orders you have taken and lets them walk the range by incrementing the number. That is an enumeration risk and a commercial-intelligence leak, and it is a reason to keep sequential integers off your public API surface even when they are the right internal key. If you are designing that surface deliberately, the key type belongs in the contract discussion alongside the rest of your API design decisions, not left to whatever the ORM defaults to.
A UUIDv7 is safe to enumerate against but not entirely opaque: because the timestamp is in the clear, anyone holding the ID can read the millisecond a record was created with uuid_extract_timestamp(). For most systems that is harmless. If creation time is itself sensitive — say the moment an account or a transaction was opened — treat UUIDv7 as semi-public and reach for a random UUIDv4, or a separate opaque public identifier, on that specific resource.
A recommendation per workload
- Single-writer, internal-only, high write volume: bigint identity. It is the smallest, the fastest to insert, and the count leak is irrelevant if the key never leaves your own systems.
- Keys exposed in URLs, or IDs minted across services and merged later: UUIDv7. You get distributed generation, no enumeration risk and index locality close enough to bigint that the write cost is easy to accept.
- Creation time is sensitive, or you need a fully opaque public handle: random UUIDv4, and budget for the index bloat — or keep an internal ordered key and expose a separate random one.
A common pattern in analytics-heavy estates is a bigint surrogate for internal joins plus a UUIDv7 for anything that crosses a service or a warehouse boundary — worth deciding early, because the key type is what everything downstream, including how records land in a separate analytics platform, ends up keyed on. Whatever you choose, make it a decision with a reason attached, not a default you discover in production when the index has already doubled in size.
Build and rescue work
Hands-on delivery of this kind is handled by Sixteen Pillars Studio.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Can you trust the architecture you have?
Architecture diagrams rarely show the reality of how systems actually operate. An independent review establishes what is really there.