PostgreSQL 18 UUID Primary Keys: What to Change Now

Direct answer: For most new backends on PostgreSQL 18, use time-ordered UUIDv7 as the primary key instead of random UUIDv4. Time-ordered keys insert near the right edge of the B-tree, which cuts page splits, index bloat and write amplification compared with fully random keys. If your tables are small, keep what you have and fix it later. If a large, write-heavy table is already on random UUIDs and showing slow inserts or a bloated primary key index, plan a migration now, before data volume makes it expensive.
Last updated: November 2026
Key Takeaways
- Random UUIDs scatter inserts across the whole primary key index, while UUIDv7 keeps them mostly sequential.
- UUIDv7 gives you the write behavior of an ordered key without giving up globally unique, client-generatable IDs.
- UUIDv7 embeds a creation timestamp, so do not use it where exposing creation time is a privacy problem.
- Small tables rarely justify a migration; large, write-heavy tables on random UUIDs usually do.
- A new API backend should pick its key strategy on day one, because changing a primary key later touches every foreign key and client.
What happened and why it matters for PostgreSQL 18 UUID primary keys
ADTmag published a piece on October 30, 2026 titled PostgreSQL 18 Gives Developers New Reasons to Revisit Indexes and UUID Keys. We could not retrieve the full text, so we will not claim to know what it argues beyond the headline. The headline is enough to explain the timing: teams are asking whether their key and index choices still make sense.
The question matters because PostgreSQL 18 UUID primary keys are a decision that is cheap on day one and painful on day 500. Once a table holds hundreds of millions of rows, with foreign keys, replicas and external clients depending on the ID format, changing it is a project, not a migration script. This article covers what to choose, what it costs, and when to act.
Why do random UUIDs hurt PostgreSQL indexes?
Random UUIDs (version 4) hurt because every new row lands in an unpredictable spot in the primary key B-tree. A sequential key always appends to the rightmost leaf page, which stays hot in memory. A random key touches any leaf page, so the database keeps pulling cold pages into cache just to insert one entry.
This has three practical consequences:
- Page splits and bloat. Inserting into the middle of full pages forces splits, leaving many pages partly empty. The index grows larger than the data justifies.
- Cache pressure. The whole index becomes the working set for writes, so once it outgrows memory, inserts start waiting on disk.
- More write-ahead log. Touching many different pages after a checkpoint generates more full-page writes, which increases WAL volume and replication traffic.
None of this shows up on a table with 50,000 rows. It shows up when the index no longer fits in RAM, usually at the moment your product is finally growing.
What is UUIDv7 and why does it fix most of that?
UUIDv7 is a UUID whose leading bits are a millisecond timestamp, followed by random bits. IDs generated later sort after IDs generated earlier, so new rows cluster at the right edge of the index, much like a bigserial. You keep the 128-bit, globally unique format that clients and services can generate without asking the database.
PostgreSQL 18 adds a native uuidv7() function, so you no longer need an extension or application-side library just to get time-ordered IDs. Check the official release notes for the exact behavior in your version, and test it on your own workload instead of trusting a general rule.
There are two tradeoffs worth stating plainly:
- The timestamp is visible. Anyone with an ID can read roughly when the record was created. For orders and internal objects that is fine. For anything where creation time is sensitive, think twice.
- Ordering is only approximate across machines. If several app servers generate IDs, clock differences mean strict ordering is not guaranteed. Do not use the ID as a substitute for a real
created_atcolumn when exact order matters.
UUIDv7 vs random UUID vs bigint: which key should you use?
For most API backends, UUIDv7 is the best default, and bigint identity columns remain a strong choice when IDs never leave your trust boundary. Random UUIDv4 is now the weakest default for large, write-heavy tables.
| Key type | Insert locality | Size | Client-generated | Best for |
|---|---|---|---|---|
| bigint identity | Sequential | 8 bytes | No | Internal tables, simple monoliths |
| UUIDv7 | Mostly sequential | 16 bytes | Yes | Public APIs, distributed services, offline clients |
| UUIDv4 (random) | Scattered | 16 bytes | Yes | Small tables, IDs that must reveal nothing |
Note the size row. A UUID is twice the size of a bigint, and that cost repeats in every secondary index and every foreign key column. If you have no need for client-generated or non-guessable IDs, a bigint is smaller and faster. UUIDv7 is the compromise when you want UUID benefits without the random-insert penalty.
Also remember that unguessable IDs are not access control. Whatever key you use, enforce authorization on every request, as covered in our guide to API rate limiting and authentication.
Should a startup migrate existing UUID keys or design for UUIDv7 now?
If you are building a new backend, design for UUIDv7 now. If you already run random UUIDs, migrate only the tables where the evidence says it matters. Migration has real cost, so earn it with measurements.
Use this rule of thumb:
- New project: choose UUIDv7 or bigint on day one. Do not start with random UUIDs by habit.
- Existing tables under a few million rows: leave them. Switch only the default for new tables.
- Large, append-heavy tables (events, logs, orders, messages): these benefit most. Measure first, then migrate if the index is bloated or insert latency is climbing.
- Tables updated far more than inserted: key ordering helps less. Look at other causes before blaming the key.
This is one of the early signals in 5 signs your backend won’t scale past 10,000 users: problems that are invisible at launch and expensive at growth. Picking the key now is the cheap version of that lesson.
How do you migrate to UUIDv7 without breaking clients?
You do not need to rewrite old IDs. UUIDv7 and UUIDv4 values are both valid UUIDs, so the column type stays the same. The safest approach is to change how new IDs are generated and leave existing rows alone.
- Measure the baseline. Record primary key index size, table size, insert latency percentiles and WAL volume for the target table.
- Switch the default. Change the column default to
uuidv7(), and update any application code that generates UUIDv4 values. - Keep old rows as they are. Mixed keys work fine. New rows will append at the right edge; old rows stay scattered.
- Rebuild the bloated index. Use
REINDEX INDEX CONCURRENTLYto reclaim space from past splits without blocking writes. - Compare against the baseline. Check the same metrics after a week of real traffic.
- Only then consider rewriting old IDs. This is rarely worth it, because it breaks every external reference.
If your API exposes these IDs, the format is already part of your contract. Because the type does not change, this is usually safe, but review it with the same care you would give any API versioning decision.
What other index changes should you review while you are there?
The key is only one index. The ADTmag headline pairs UUID keys with indexes in general, which is a reasonable prompt for a wider audit. Do it in the same pass, since you will already have baselines in hand.
- Find unused indexes. Every index slows writes. Check usage statistics and drop what no query touches.
- Find duplicate and overlapping indexes. An index on (a) is often redundant if you have one on (a, b).
- Check multicolumn index order. Look at the PostgreSQL 18 release notes for any changes in how multicolumn indexes can be used, and re-test your slowest queries.
- Use partial indexes. If most queries filter on an active status, index only those rows.
- Watch foreign key columns. Wide UUID foreign keys multiply index size. Index them only where joins or deletes need it.
If your database is already under strain, the choice of engine and architecture matters as much as the key. Our breakdown of the best database for a high-load backend covers that wider decision.
The verdict for founders and product owners
Treat key strategy as an architecture decision, not a detail for later. For a new API backend on PostgreSQL 18, default to UUIDv7 for public or distributed IDs and bigint for purely internal tables. Avoid random UUIDv4 on any table you expect to grow large and take heavy writes.
For an existing system, do not migrate on fashion. Measure index size and insert latency, change the default for new rows, reindex concurrently, and verify the result. That sequence is low risk, reversible in most cases, and gives you evidence before you commit to anything bigger.
Frequently Asked Questions
Is UUIDv7 better than UUIDv4 for PostgreSQL primary keys?
For write-heavy tables, usually yes. UUIDv7 is time-ordered, so inserts cluster at the end of the index instead of scattering across it, which reduces page splits and bloat. UUIDv4 is still fine for small tables or where IDs must reveal nothing about creation time.
Does PostgreSQL 18 have built-in UUIDv7 support?
PostgreSQL 18 adds a native uuidv7() function, so you no longer need an extension or application library to generate time-ordered UUIDs. Confirm details in the official release notes for the version you run.
Should I use bigint or UUID for my primary key?
Use bigint when IDs stay internal and you want the smallest, fastest keys. Use UUIDv7 when IDs are public, generated by clients or several services, or must be hard to enumerate. A UUID is 16 bytes versus 8, and that cost repeats in every index and foreign key.
Do I need to rewrite existing UUIDs to adopt UUIDv7?
No. Both formats are valid UUIDs in the same column type, so you can change the default for new rows and leave old rows untouched. Run REINDEX CONCURRENTLY to recover space from past splits, and rewrite old IDs only if you have a measured reason.
Does UUIDv7 leak information?
Yes, it embeds a millisecond creation timestamp, so anyone holding an ID can estimate when the record was created. That is acceptable for most business objects but not for cases where creation time is sensitive. It also does not replace authorization checks.
Sources
- PostgreSQL 18 Gives Developers New Reasons to Revisit Indexes and UUID Keys — ADTmag, October 30, 2026
Key and index decisions are easiest to get right while the schema is still small. If you are planning a new API or want an audit of an existing PostgreSQL backend before growth makes changes costly, our backend API development services cover schema design, indexing, and safe migrations.
About the author
Ready to start? See our Backend API Development Services, get in touch, or order on Fiverr.