Live data from Hacker News

Avoid UUID Version 4 Primary Keys in Postgres

andyatkinson.com

421–430 of 463 posts

Re: Avoid UUID Version 4 Primary Keys in Postgres

#421

Earlier quoted context omitted.

> If you have a primary key whose generation is truly random such that each number is equally likely, then that b-tree is always going to be balanced. Balanced and uniformly scattered. A random index means fetching a random page for every item. Fine if your access patterns are truly random, but that's rarely the case. > Why are you clustering on a random opaque key? InnoDB clusters by the PK if there is one, and that…

I feel, honestly, like while you are indeed correct for most cases it’s absolutely fine to use some flavor of uuid. I feel like the benefits outweighs the cost in most cases.

Sure, and for many cases, uuidv7 is that flavor. It just comes with a timestamp, which may or may not be an issue. It isn't an issue for me, which is why I use it myself.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#422

Earlier quoted context omitted.

> If the only reason you need a surrogate key is to introduce indirection in your internal database design then sequence numbers are enough. There is no need to use UUIDs. The UUID would be an example of an external key (for e.g. preventing crawling keys being easy). This article mentions a few reasons why you may later decide there are better external keys. > When I come to you and say "My name is X, this is my phon…

> The UUID would be an example of an external key (for e.g. preventing crawling keys being easy). This article mentions a few reasons why you may later decide there are better external keys. So we are talking about "external" keys (ie. visible outside the database). We are back to square one: externally visible surrogate keys are problematic because they are detached from real world information they are supposed to i…

> It does not matter if they are random or not.

Again, sometimes it does, the article lists a few of them. Making it harder to scrape, unifying across databases that share a keyspace, etc.

> And how does surrogate key help? I don't know the surrogate key that identifies my records in your database. Even if you use them internally it is an implementation detail.

That surrogate key is linked to literally every other record in the database I have for you. There are near infinite ways for me to convert something you know to that surrogate key. Give me a transaction ID, give me a phone number/email and the rough date you signed up, hell give me your IP address and I can probably work back to a user ID from auth logs.

The point isn't that you know the surrogate key, it's that _everything_ is linked to that surrogate key so if you can give me literally any info you know I can work back to the internal ID.

> This complexity is there whether you want it or not and you're not going to eliminate it with surrogate keys. It has to be explicitly taken care of.

Okay, then lets do an exercise here. A user gives you a transaction ID, and you have to tell them the date they signed up and the date you first billed them. I think yours is going to be way more complicated.

Mine is just something like:

SELECT user_id FROM transactions WHERE transaction_id=X; SELECT transaction_date FROM transactions WHERE user_id=Y ORDER BY transaction_date ASC LIMIT 1; SELECT signup_date FROM users WHERE user_id=Y;

Could be a single query, but you get the idea.

> DBMSes provide means to tackle this essential complexity: bi-temporal extensions, views, materialized views etc.

This kind of proves my point. If you need bi-temporal extensions and materialized views to tell a user what their email address is from a transaction ID, I cannot imagine the absolute mountain of SQL it takes to do something more complicated like calculating revenue per user.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#423
post #307

Earlier quoted context omitted.

I read it (and regret it is a waste of my time). Their arguments are: * integer keys are faster; * uuidv7 keys are faster; * if you want obfuscated keys, using integer and do some your own obfuscation (!!!). I can get on-board of uuidv7 (with the trade-off, of course, on stronger guessability). The integer keys argument is strange. At that point, you need to come up with a custom-built system to avoid id collision in…

You can hand out chunks of sequential ids from a central coordinator to avoid collision; this is a well-established pattern. Re: natural keys (or something like it), I was using it as an example of how badly PK choice can impact performance at scale.

> You can hand out chunks of sequential ids from a central coordinator to avoid collision; this is a well-established pattern.

The problem is: is that part of postgresql? If not, someone has to write the buggy code for that well-established pattern. (BTW, I honestly think autoincrement is fine and the choice of PK is so minor you can always pay your way to solve it if you really have a problem at scale).

Re: Avoid UUID Version 4 Primary Keys in Postgres

#424

Counterargument... I do technical diligence so I talk to a lot of companies at points of inflection, and I also talk to lots who are stuck. The ability to rapidly shard everything can be extremely valuable. The difference between "we can shard on a dime" and "sharding will take a bunch of careful work" can be expensive If the company has poor margins, this can be the difference between "can scale easily" and "we're n…

I also do technical diligence, often times the blocker to sharding is the target company's tenancy model and schema. PK data type is certainly a blocker.

Definitely an issue, rarely the main one. You can work around integer PKs with composite keys or offset-based sharding schemes. What you can't easily fix is a schema with cross-tenant foreign keys, shared lookup tables, or a tenancy model that wasn't designed for data isolation from day one. Those are architectural decisions that require months of migration work.

UUIDs buy you flexibility, sure. But if your data model assumes everything lives in one database, the PK type is a sub-probem of your problems.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#425
post #387

Earlier quoted context omitted.

The person I was responding to was talking about gender, but if you want to talk about biology then https://en.wikipedia.org/wiki/Intersex#Prevalence https://en.wikipedia.org/wiki/Klinefelter_syndrome https://en.wikipedia.org/wiki/XXYY_syndrome https://en.wikipedia.org/wiki/XXXY_syndrome https://en.wikipedia.org/wiki/XXXYY_syndrome https://en.wikipedia.org/wiki/XXXXY_syndrome https://en.wikipedia.org/wiki/Trisomy_X I…

Not only have you undermined your claim to a Nobel award by showing a spurious understanding of biology, you wrote, quite sarcastically "it is impossible for humanity to discover new information that updates our world model". Well then, we will all await your discovery of that 3rd gamete, or some theory so innovative that it tips this well studied, well understood, uncontested (by any valid competitor) model to the w…

I was being sarcastic, the thread started about gender and you moved it to gametes. Gender is a social construct as we can observe by the fact that what gender _is_ isn't consistent across cultures.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#426
post #425

Earlier quoted context omitted.

Not only have you undermined your claim to a Nobel award by showing a spurious understanding of biology, you wrote, quite sarcastically "it is impossible for humanity to discover new information that updates our world model". Well then, we will all await your discovery of that 3rd gamete, or some theory so innovative that it tips this well studied, well understood, uncontested (by any valid competitor) model to the w…

I was being sarcastic, the thread started about gender and you moved it to gametes. Gender is a social construct as we can observe by the fact that what gender _is_ isn't consistent across cultures.

I keep addressing your points and you keep moaning about other people. Since sex and gender are not different until you are able to provide some reason that they are beyond bare assertion then gametes are relevant.

> you could tell us all what the scientific discovery was that made gender separate from sex, who found it and when, and what the defining difference is. Did they win a Nobel for that?

Take your time, but please avoid making me restate what I've written along with the obvious implications simply because you find it all too inconvenient to address.

Re: Avoid UUID Version 4 Primary Keys in Postgres

#427
post #425

Earlier quoted context omitted.

I was being sarcastic, the thread started about gender and you moved it to gametes. Gender is a social construct as we can observe by the fact that what gender _is_ isn't consistent across cultures.

I keep addressing your points and you keep moaning about other people. Since sex and gender are not different until you are able to provide some reason that they are beyond bare assertion then gametes are relevant. > you could tell us all what the scientific discovery was that made gender separate from sex, who found it and when, and what the defining difference is. Did they win a Nobel for that? Take your time, but…

> I keep addressing your points…

No you did not.

> Since sex and gender are not different until you are able to provide some reason that they are beyond bare assertion then gametes are relevant.

Sex is a parameter of biology, gender is a parameter of social constructs.

You are also having bare assertions that they are the same. Gametes are not relevant. You are unable to discern between different values.

Also stop bringing up the Nobel prize like it matters for the conversation. You are the one who interjected it into the conversation.

Edit: added after the post. To make sure I am not speaking to a bot, can you tell me who the first person in this thread was that mentioned the word “gamete”

Re: Avoid UUID Version 4 Primary Keys in Postgres

#428
post #388

Earlier quoted context omitted.

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.

No, if its randomly distributed then every specific configuration has the same exact chance of happening. I am laughing at all the people coming out of the woodwork to reply to my original post in this thread misunderstanding randomness and chance. If you flip a coin a million times and it lands on head every single time, the millionth and 1 time still has a 50/50 chance of landing on heads

No, because most likely the coin wasn’t a fair coin then, or there was some other bias going on

Re: Avoid UUID Version 4 Primary Keys in Postgres

#429
post #369

Earlier quoted context omitted.

> 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 onl…

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

> Nope, it's also the fact that it is ONE configuration, whereas all the rest are much much larger number.

That is the human pattern overactive pattern matching at play. I compared the single configuration of all dots on one location to any other specific configuration. You are not comparing to to _every other configuration_ because they are not the same

You are assigning specific importance to a single valid set of randomly selected data, because it seems significant to our brains.

If I asked you to give me an array of 1 million items containing an x, and y coordinate, what are the odds that any single specific set of items are returned?

Based on your answer to that, what are the odds for a set being return with all the same exact x and y coordinates, and a set with different x, and y coordinates?

if you answer anything other than it being the same chance, then you either don't think the selection mechanism is random, or you are falling to the standard fallacies around randomness

Re: Avoid UUID Version 4 Primary Keys in Postgres

#430
post #388

Earlier quoted context omitted.

No, if its randomly distributed then every specific configuration has the same exact chance of happening. I am laughing at all the people coming out of the woodwork to reply to my original post in this thread misunderstanding randomness and chance. If you flip a coin a million times and it lands on head every single time, the millionth and 1 time still has a 50/50 chance of landing on heads

No, because most likely the coin wasn’t a fair coin then, or there was some other bias going on

Im talking about true random. If you believe there is a bias, then you dont believe its a random selection
Post reply on HN