The letter S in a light blue, stylized speech bubble followed by SpeakBits
SpeakBitsThe letter S in a light blue, stylized speech bubble followed by SpeakBits
Trending
Top
New
Controversial
Search
Groups

Enjoying SpeakBits?

Support the development of it by donating to Patreon or Ko-Fi.
About
Rules
Terms
Privacy
EULA
Cookies
Blog
Have feedback? We'd love to hear it!

PostgreSQL and UUID as primary key

maciejwalkowiak.com
submitted
a year ago
byjustadevtoprogramming

Summary

UUIDs are easy to generate, easy to share between distributed systems and guarantee uniqueness. Postgres has a dedicated data type for UUIDs: uuid. UUID is a 128 bit data type, so storing single value takes 16 bytes. Storing string takes 1 or 4 bytes overhead plus storing the actual string.

Java's UUID.randomUUID() - returns UUID v4 - which is a pseudo-random value. For us the more interesting one is UUIDv7 - which produces time-sorted values. Each time new Uuid v7 is generated, a greater value it has. And that makes it a good fit for B-Tree index.

 web site website internet site site envelope honeycomb jersey T-shirt tee shirt-0
12

4 Comments

2
joesch
a year ago
I've never been fond of using anything other than the primary id
2
justadevOP
a year ago
It has its places, especially if you want to obfuscate the sequence of the content
2
joesch
a year ago
It seems like it would be best to make an id based on inherent data and not introduce something just for id purposes
2
justadevOP
a year ago
If the data warrants it, yes. You don't always get that though