It’s really just in comparison to securely random opaque IDs like UUIDv4. There’s discussions about this online like on hackernews but it’s pretty nuanced. It’s basically things like providing extra information about a user’s habits or events related to them (eg if the IDs of records are available but the record themselves are protected, and the devs don’t realise the IDs leak that minor information), or being able to guess account creation times without needing to have read permission on a user’s profile/bio, etc.
But… we live in a world where most apps have also used auto incrementing integer PKs as slugs, so… probably not a huge deal relatively speaking.
> but once your code relies on IDs being in creation order, a stray v4 ID is a correctness bug, not a style problem.
There's a performance problem with mixing v4 and v7 in the same index too. Until ~2039 every v7 starts with 0x01, so ~99% of v4 values sort after all of your v7s.
When Postgres splits B-tree pages, the page being split is the rightmost leaf, it fills the left page to the fillfactor (90% by default). That's the normal case for v7, since new IDs always go to the right end. Any other page splits roughly 50/50. Once there are more v4s than fit on about one leaf page, new v7s land just before the v4 block instead of on the rightmost page. Every split then leaves behind a half-empty page that will never be filled, so you get a larger, sparser index that needs REINDEX CONCURRENTLY or pg_repack from time to time.
I discovered this by switching from v4 to v7 and expecting a nicer packed index. Sadly we need to periodically `REINDEX CONCURRENTLY` to keep the index neat.
6
u/harun_91 8d ago
This is a great article the only thing that kinda feels of (at least for me) is:
The trade-off is that the ID reveals when the record was created. If that information is sensitive for a given table, UUIDv7 isn't the right choice.
Why would the timestamp of a record would be sensitive? TBF I can't come up with a valid reason in my head.
13
u/dkam 8d ago
I enjoyed your post!
> but once your code relies on IDs being in creation order, a stray v4 ID is a correctness bug, not a style problem.
There's a performance problem with mixing v4 and v7 in the same index too. Until ~2039 every v7 starts with 0x01, so ~99% of v4 values sort after all of your v7s.
When Postgres splits B-tree pages, the page being split is the rightmost leaf, it fills the left page to the fillfactor (90% by default). That's the normal case for v7, since new IDs always go to the right end. Any other page splits roughly 50/50. Once there are more v4s than fit on about one leaf page, new v7s land just before the v4 block instead of on the rightmost page. Every split then leaves behind a half-empty page that will never be filled, so you get a larger, sparser index that needs REINDEX CONCURRENTLY or pg_repack from time to time.
I discovered this by switching from v4 to v7 and expecting a nicer packed index. Sadly we need to periodically `REINDEX CONCURRENTLY` to keep the index neat.