Skip to content

feat: add sub-millisecond timestamp into uuid_generate_v7#42

Open
thme-tjpr wants to merge 1 commit into
fboulnois:mainfrom
thme-tjpr:main
Open

feat: add sub-millisecond timestamp into uuid_generate_v7#42
thme-tjpr wants to merge 1 commit into
fboulnois:mainfrom
thme-tjpr:main

Conversation

@thme-tjpr

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

Copy link
Copy Markdown

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

SakaSun commented Nov 27, 2024

Copy link
Copy Markdown

+1
This is a nice feature to be added.

@reshke

reshke commented Jul 2, 2026

Copy link
Copy Markdown

@fboulnois can you please take a look of it? It would be nice to generate uuidv7 in postgres < 18 (via this extension) exactly like in core(>=18)

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.

3 participants