Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sub-millisecond timestamp into uuid_generate_v7 #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thme-tjpr
Copy link

@thme-tjpr thme-tjpr commented Nov 27, 2024

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

@SakaSun
Copy link

SakaSun commented Nov 27, 2024

+1
This is a nice feature to be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants