feat: add sub-millisecond timestamp into uuid_generate_v7 #42
+25
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch add a sub-millisecond precision to uuid 7, as provided in the rfc 9562.
Using an OPTIONAL sub-millisecond timestamp fraction (12 bits at maximum) as per Section 6.2 (Method 3). https://datatracker.ietf.org/doc/rfc9562/
Some applications need this kind of precision, especially when we include multiple records in batch and need to ensure that the ids are ordered by time.
Ex:
SELECT id, uuid_generate_v7() uuid from generate_series(1, 1000) as id order by uuid;
When we use 250*nanoseconds precision like in this patch, all the uuids are generated in sequence.
The original code return unsorted data like:
id uuid
19 01936e5d-d97e-700c-8dbc-23b886d51ffb
270 01936e5d-d97e-701d-91ac-0596339033cf
374 01936e5d-d97e-7020-bea1-0decee141a79
521 01936e5d-d97e-702e-bac9-9d83a0c35418
The patched code return sorted data like this:
id uuid
1 01936e62-33a0-7ea1-abb5-cc3197098a83
2 01936e62-33a0-7eaa-a8e5-59f6c28355e9
3 01936e62-33a0-7eb3-9aa1-f09dcffa352f
4 01936e62-33a0-7ebc-b099-a31e1d652135