by stonemetal12 · 1y ago · 0 comments
>optional constructs
So it is explicitly mentioned in the RFC as optional, and Pg doesn't state that they guaranty that option. The point still stands, depending on optional behavior is a recipe for failure when the option is no longer taken.
Implementations that need monotonicity beyond the resolution of a timestamp-- like when you allocate 30 UUIDs at one instant in a batch-- can optionally use those additional bits for that purpose.
> Implementations SHOULD employ the following methods for single-node UUID implementations that require batch UUID creation or are otherwise concerned about monotonicity with high-frequency UUID generation.
(And it goes on to recommend the obvious things you'd do: use a counter in those bits when assigning a batch; use more bits of time precision; etc.)
The comment in PostgreSQL before the implementation makes it clear that they chose the third option for this in the RFC:
* variant bits. To ensure monotonicity in scenarios of high-
* frequency UUID generation, we employ the method "Replace
* LeftmostRandom Bits with Increased Clock Precision (Method 3)",
* described in the RFC. ...It's explicitly partially monotonic.
Or as other people would call it, "not monotonic".
People are talking past each other based on their use of the word "monotonic".
"might generate two IDs in the same millisecond" is not a very exotic occurrence. It makes a big difference whether the rest is guaranteed or not.
> And PostgreSQL used one of these recommended approaches and documented it.
Well that's the center of the issue, right? OP's interpretation was that PostgreSQL did not document such, and so it shouldn't be relied upon. If it is a documented promise, then things are fine.
But is it actually in the documentation? A source code comment saying it uses a certain method isn't a promise that it will always use that method.
From the RFC:
Monotonicity (each subsequent value being greater than the last) is
the backbone of time-based sortable UUIDs. Normally, time-based UUIDs
from this document will be monotonic due to an embedded timestamp
"time-based UUIDs from this document will be monotonic". "will be monotonic".I'm not sure how much more explicit this can be made.
The intent of UUIDv7 is monotonicity. If an implementation is not monotonic then it's a bug in the implementation.
The use of rand_a for extra monotonicity is optional. The monotonicity itself is not optional.
§5.7 states:
Alternatively, implementations MAY fill the 74 bits,
jointly, with a combination of the following subfields,
in this order from the most significant bits to the least,
to guarantee additional monotonicity within a millisecond:
Guaranteeing additional monotonicity means that there is already a 'base' level of monotonicity, and there are provisions for even more ("additional") levels of it. This 'base level' is why §6.2 states: Monotonicity (each subsequent value being greater than the last) is
the backbone of time-based sortable UUIDs. Normally, time-based UUIDs
from this document will be monotonic due to an embedded timestamp;
however, implementations can guarantee additional monotonicity via
the concepts covered in this section.
"Backbone of time-based sortable UUIDs"; "additional monotonicity". Additional: adding to what's already there.Or to put it another way: OP is suggesting you don't depend on it being properly monotonic, because the default is that it is only partially monotonic.
“Normally, I am at home because I do not have a reason to go out; however, sometimes I am at home because I am sleeping.”
Notice how this statement does not actually mean that I am always at home.
> implementations can guarantee additional monotonicity via the concepts covered in this section
So, no, the “time part” of the postgres implementation is, in part, one of the options discussed in the spec, not merely the “time part” required in the spec.
Even if the behavior went away, UUIDs unlike serials can always be safely generated directly by the application just as well as they can be generated by the database.
Going straight for that would arguably be the "better" path, and allows mocking PRNG to get sequential IDs.
Sometimes the best you can do is recognize who you're working with today, know how they work, and be prepared for those people to be different in the future (or of a different mind) and for things to change regardless to expressed guarantees.
....unless we're talking about the laws of physics... ...that's different...