Quick revision
The short answer
A bigint sequence key is 8 bytes, ordered and cheap to index and join, but the database must hand it out and it reveals order and volume. A UUID is 16 bytes and can be created anywhere without coordination, which suits distributed and offline systems, but random UUIDv4 values scatter inserts across the index. UUIDv7 puts a timestamp in the first bits, so new keys arrive in roughly increasing order and most of that index cost disappears.
What to remember
In this answer, we saw that bigint sequence keys are compact, ordered and fast but need the database to allocate them and expose order and volume, while UUIDs cost 16 bytes but can be generated anywhere. Random UUIDv4 values scatter inserts, causing page splits, cache misses and larger WAL, and UUIDv7 fixes most of that by putting a timestamp first, with uuidv7() built into PostgreSQL 18. Whatever we pick, we store UUIDs in the native uuid type.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A bigint sequence key is 8 bytes, ordered and cheap to index and join, but the database must hand it out and it reveals order and volume. A UUID is 16 bytes and can be created anywhere without coordination, which suits distributed and offline systems, but random UUIDv4 values scatter inserts across the index. UUIDv7 puts a timestamp in the first bits, so new keys arrive in roughly increasing order and most of that index cost disappears.
Why this question is asked
Choosing a primary key type looks like a small decision, but it is hard to change later, because the key spreads into foreign keys, indexes, URLs, caches and events. Interviewers ask this question to see whether we understand what the key does to the index and the disk, not just which one is fashionable. In this answer, we will recap how each key is generated, look at what happens inside a B-tree index on insert, see what UUIDv7 changes, cover storage, and finish with when to choose each.
1A Quick Recap of the Two Key Types
A sequence is a database object that hands out increasing numbers. In PostgreSQL we usually write id bigint GENERATED ALWAYS AS IDENTITY, which creates a sequence behind the column. Each insert asks the sequence for the next value: 1, 2, 3 and so on. A bigint takes 8 bytes and will not run out in any realistic lifetime.
A UUID, a universally unique identifier, is a 128-bit value, 16 bytes, usually written as 36 characters of hex with dashes. Its uniqueness comes from randomness or from a timestamp plus randomness, not from a central counter. That means a mobile app, a service in another region, or a batch job can create ids without asking the database.
2What Happens Inside the Index on Insert
The primary key has a B-tree index, which keeps keys sorted in pages of 8 KB in PostgreSQL. With a sequence, every new key is larger than all earlier keys, so every insert goes to the rightmost leaf page. That page stays in memory, fills up, and a new one starts. Inserts are cheap and the index stays densely packed.
Random UUIDv4 values are the opposite. Each new key belongs somewhere random in the sorted order, so inserts land on pages all over the index. With a large table most of those pages are not in memory, so the database must read them from disk first. Full pages must split in half to make room, which leaves the index about half empty in places and produces extra writes. And after each checkpoint PostgreSQL writes the full image of every page it touches the first time into the WAL, the write-ahead log, so touching many different pages makes the WAL much bigger.
| Key type | Pages touched | Cache behaviour | WAL effect |
|---|---|---|---|
| bigint sequence | a handful at the right edge | hot pages stay in memory | small |
| UUIDv4 (random) | up to 1,000 different leaf pages | many reads from disk | many full-page images |
| UUIDv7 (time first) | a handful near the right edge | like a sequence | small |
3What UUIDv7 Changes
UUIDv7, defined in RFC 9562, puts a 48-bit Unix timestamp in milliseconds at the front of the value and fills the rest with random bits (with a few bits marking the version). Because the timestamp comes first and B-tree order compares bytes from the front, ids created later sort after ids created earlier. Inserts now behave almost like a sequence: they go to the right edge of the index, and the locality problem is mostly gone, while ids can still be created anywhere.
On older PostgreSQL versions, we generate UUIDv7 in the application, for example with a small library in Java, since java.util.UUID.randomUUID() only creates version 4. The ordering is only rough: two services with slightly different clocks produce interleaved ids, and ids created in the same millisecond are ordered by their random part unless the generator adds a counter. That is fine for index locality, but we should never use a UUIDv7 as a precise ordering of events.
4Store It as uuid, Not Text
PostgreSQL has a native uuid type that stores the 16 bytes directly. Storing the same value in a text or varchar column costs 36 characters plus a length header, about 37 bytes, more than twice the size, and comparisons become string comparisons that depend on collation. Every foreign key and every index that holds the key pays that size again.
So the column type is uuid, both for the primary key and for every column that refers to it. In Java, the driver maps it to java.util.UUID directly.
5Choosing Between Them
A bigint sequence is the right default for a single database where the application inserts through the database anyway: it is the smallest, fastest key, joins compare 8 bytes, and every secondary index stays compact. Its weaknesses are that ids exist only after the insert, merging two databases with overlapping ranges is painful, and public ids such as /orders/1043 reveal how many orders we have and let people guess other ids.
UUIDs fit when ids must be created without the database: in distributed services, by offline clients that sync later, in event-driven flows where the id is needed before the row is written, or when data from several sources will be merged. If we choose UUIDs today, we should choose UUIDv7 rather than v4 for the primary key. A common middle path keeps a bigint primary key internally and adds a unique UUID column for public use.
6Follow-up questions
Does UUIDv7 leak anything compared with UUIDv4?
Yes, it leaks the creation time to the millisecond, because the timestamp sits in the first 48 bits and anyone can decode it. For an order id that is usually harmless, but for something like a user id it can reveal when an account was created, and comparing two ids shows which came first. It does not reveal volume the way a sequence does, and the random part still makes ids hard to guess. If even the time is sensitive, we use UUIDv4 for public ids, or keep UUIDv7 internal.
Is a sequence a bottleneck for very high insert rates?
Rarely. Sequence calls are not part of the transaction and PostgreSQL can cache values per session with the CACHE option, and ORMs fetch blocks of ids at once. The real limit appears only when several databases must create ids independently, and then UUIDs solve a coordination problem rather than a speed problem.
Are UUID collisions a real risk?
Not in practice. UUIDv4 has 122 random bits and UUIDv7 still has about 74, so the chance of a collision is negligible even at billions of ids. The primary key constraint would catch one anyway.
7Conclusion
In this answer, we saw that bigint sequence keys are compact, ordered and fast but need the database to allocate them and expose order and volume, while UUIDs cost 16 bytes but can be generated anywhere. Random UUIDv4 values scatter inserts, causing page splits, cache misses and larger WAL, and UUIDv7 fixes most of that by putting a timestamp first, with uuidv7() built into PostgreSQL 18. Whatever we pick, we store UUIDs in the native uuid type.
Open with
A bigint sequence key is 8 bytes and always increasing, so inserts go to the right edge of the B-tree, indexes stay compact and joins are fast. The downsides are that the database has to allocate it, so the id exists only after the insert, merging databases is awkward, and exposed ids reveal ordering and volume.
Then, if they want more
A UUID is 16 bytes and can be generated anywhere without coordination, which helps with distributed services, offline clients and merging data. The catch with random UUIDv4 is that each insert lands at a random place in the index, so on a big table we get page splits, poor cache locality and more WAL because of full-page writes.
UUIDv7 puts a millisecond timestamp in the high bits, so new ids are roughly increasing and insert behaviour is close to a sequence. PostgreSQL 18 has a uuidv7() function; on older versions I generate it in the application. It does leak the creation time, so I think about that for public ids.
I always store UUIDs in the native uuid type, never text, which would be 36 characters. My default in a single database is bigint, and if ids must be created outside the database, UUIDv7.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 18
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare bigint sequence keys and UUID keys for a PostgreSQL table. When would you choose...": https://scrivio-interview-library.netlify.app/databases/data-modeling/05511e2db5/