Live data from Hacker News

Avoid UUID Version 4 Primary Keys in Postgres

andyatkinson.com

371–380 of 463 posts

Re: Avoid UUID Version 4 Primary Keys in Postgres

#371

Earlier quoted context omitted.

At my company we only use UUIDs as PKs. Main reason I use it is the German Tank problem: https://en.wikipedia.org/wiki/German_tank_problem (tl;dr; prevent someone from counting how many records you have in that table)

This was a great read, thank you for sharing!

Appreciate it!

Re: Avoid UUID Version 4 Primary Keys in Postgres

#372

This is incredibly database-specific. In Postgres random PKs are bad. But in distributed databases like Cockroach, Google Cloud Datastore, and Spanner it is the opposite - monotonic PKs are bad. You want to distribute load across the keyspace so you avoid hot shards.

Even in a distributed database you want increasing (even if not monotonic) keys since the underlying b-tree or whatever will very likely behave badly for entirely random data.

UUIDv7 is very useful for these scenarios since

A: A hash or modulus of the key will be practically random due to the lower bits being random or pseudo-random (ie distributes well between nodes)

B: the first bits are sortable.. thus the underlying storage on each node won't go bananas.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#373
post #25

Long article about why not to use UUIDv4 as Primary Keys, but.. Who is doing so? And why are they doing that? How would you solve their requirements? Just throwing out "you can use UUIDv7" doesn't help with, e.g., the size they take up. Aren't people using (big)ints are primary keys, and using UUIDs as logical keys for import/export, solving portability across different machines?

This was written based on working on several Postgres databases at different companies of “medium” size as a consultant, that had excessive IO and latency and used UUID v4 PKs/FKs. They’re definitely out there. We could transform the schema for some key tables as a demonstration with big int equivalents and show the IO latency reduction. With that said, the real world PK data type migration is costly but becomes a business decision of whether to do or not.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#374
post #3

"if you use PostgreSQL" (in the scientific reporting world this would be the perennial "in mice")

It's not just Postgres or even OLTP. For example, if you have an Iceberg table with SCD2 records, you need to regularly locate and update existing records. The more recent a record is, the more likely it is to be updated. If you use UUIDv7, you can partition your table by the key prefix. Then the bulk of your data can be efficiently skipped when applying updates.

Good addition!

Re: Avoid UUID Version 4 Primary Keys in Postgres

#375

Being able to create something and know the id of it before waiting for an http round trip simplifies enough code that I think UUIDs are worth it for me. I hadn't really considered the potential perf optimization from orderable ids before though - I will consider UUID v7 in future.

Great!

Re: Avoid UUID Version 4 Primary Keys in Postgres

#376
post #319

The counter argument I would say is that having all these integer ids comes with many problems. You can't make em public cause they leak info. They are not unique across environments. Meaning you have to spin up a lot of bs envs to just run it. But retros are for complaining about test envs, right? Uuid4 are only 224bits is a bs argument. Such a made up problem. But a fair point is that one should use a sequential uu…

Some additional cases we encounter quite often where UUIDs help: - A client used to run our app on-premises and now wants to migrate to the cloud. - Support engineers want to clone a client’s account into the dev environment to debug issues without corrupting client data. - A client wants to migrate their account to a different region (from US to EU). Merging data using UUIDs is very easy because ID collisions are pr…

If merging or moving data between environments is a regular occurrence, I agree it would be best to have non-colliding primary keys. I have done an environment move (new DB in different AWS region) with integers and sequences for maybe a 100 table DB and it’s do-able but a high cost task. At that company we also had the demo/customer preview environment concept where we needed to keep the data but move it.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#377
post #342

Earlier quoted context omitted.

That is only true if you're using an extremely idiosyncratic definition of gender. As far as 95% of English speakers are concerned, gender is defined by the body you possess.

The only real states of matter are solids, liquids, and gases. Everything else is just woke lunacy. I am confident in this fact because I learned it in elementary school decades ago and it is impossible for humanity to discover new information that updates our world model. Every English speaker knows that “plasmas” and “Bose-Eisenstein condensates” are made up.

We all await your Nobel for finding a third type of gamete.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#378
post #369

Earlier quoted context omitted.

> A million dots scattered randomly over a graph can all land on the exact same coordinate if it’s truly random. It won't happen though. 0.00000000% chance it happens even once in a trillion attempts. > What most people intuit as random is some sort of noise function that is generally dispersed and doesn’t trigger the pattern matching part of their brain Yes, people intuit the texture of random wrong in a situation w…

> It won't happen though. 0.00000000% chance it happens even once in a trillion attempts. It has the same odds as any other specific configuration of randomly assigned dots. The overly active human pattern matching behavior is the only reason it would be treated as special.

Entropy says it's special. If you have a million dots and 10,000 coordinates, you have 10,000 ways for all the dots to land in the same coordinate, and a zillion kavillion stupillion ways to have somewhere near 100 dots in each coordinate.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#379
post #229

Earlier quoted context omitted.

And then have to enter/handle a non-date through all systems? How do you know if this non-dated person is over the age of minority? Eligible for a pension? Maybe the answer is to evenly spread the defaults over 365 days.

If you don't know their birthday, you can presumably never answer that question in any case. If you only know the birth year and keyed 99 as the month for unknown, then your algorithm would determine they were of a correct age on the start of the year after that was true, which I guess would be what you want for legal compliance. If you don't even know if the birth year is correct, then the correct process depends on…

> or take the new one instead and having the responsibility to tell people their number had changed

Or have the opportunity to scam people into thinking you’re a different person. (E.g. take a $1M loan, go bankrupt, remember your birthday, and take a loan again.)

Re: Avoid UUID Version 4 Primary Keys in Postgres

#380
post #369

Earlier quoted context omitted.

> A million dots scattered randomly over a graph can all land on the exact same coordinate if it’s truly random. It won't happen though. 0.00000000% chance it happens even once in a trillion attempts. > What most people intuit as random is some sort of noise function that is generally dispersed and doesn’t trigger the pattern matching part of their brain Yes, people intuit the texture of random wrong in a situation w…

> It won't happen though. 0.00000000% chance it happens even once in a trillion attempts. It has the same odds as any other specific configuration of randomly assigned dots. The overly active human pattern matching behavior is the only reason it would be treated as special.

>It has the same odds as any other specific configuration of randomly assigned dots

Which doesn't change anything in practice, since it having "the same odds as any other specific configuration" ignores the fact that more scattered configurations are still far more numerous than it (or even from ones with more visual order in general) taken all together.

>The overly active human pattern matching behavior is the only reason it would be treated as special.

Nope, it's also the fact that it is ONE configuration, whereas all the rest are much much larger number. That's enough to make this specific configuration ultra rare in comparison (since we don't compare it to each other but to all others put together).

Post reply on HN