-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Customize UUIDv7 generation for database partitioning #130843
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
Comments
For this one, I plan to somehow make it work under #120878. I don't know how to make it work properly though because the notion of |
As for |
Using a timestamp would be okay, but the users’ mindset for this function (or at least for my use case ;-)) is "I need to create a new DB partition for today / the next month which is YYYY-MM-DD 0 o'clock. Gimme the minimal UUIDv7 for that!", so supporting datetimes would be very convenient. |
It's possible to convert the datetime object to a timestaml via .timestamp(). One reason for accepting a timestamp is essentially to make the interface more flexible for the standard library and easier to maintain (maintenance cost is something that needs to be taken into account). Also, a timestamp is timezone agnostic. I'm leaving for 10 days so I won't be able to reply except on mobile. |
I understand your reasoning and something with a timestamp is better than nothing. :-) |
We actually had a similar discussion on whether to accept or not datetime objects for gzip in #128584. I see reasons not to but I also see reasons to. I think we need to find an equilibrium between what would be the most useful and what would be the best solution for future compatibility (remember that once we decide on something for the standard library, it becomes kind of "frozen" and requires a deprecation period for any changes we make). |
It is enough to take the left segment of the required length from the UUID as the partition key. The accuracy does not necessarily have to be exactly the same as 24 hours. |
Nobody really needs version 6. Only version 1 and 7. See the uuid_extract_timestamp() function here for an example |
You still need to pass in a custom timestamp/datetime to calculate that segment for the given time and in advance. |
First of all, I've added the possibility to recover the timestamp for UUIDv7 objects as part of |
The RFC does not formally define the timestamp alteration of a UUID:
So, one way to do it, is to actually entirely ignore the stored timestamp but not the counter. In other words, if someone specifies an explicit timestamp, then they won't advance the internal global UUIDv7 clock, but they will advance the counter. The reason why advancing the timestamp is a bad idea is as follows: let's say I use the last possible UUIDv7 timestamp as my custom timestamp. Then subsequent calls to
I would prefer not exposing this. It's possible to ignore the tail bits by working with |
Here are the uuid7 related function I currently need:
Again, I could understand if this is nothing that you would put into the stdlib, and I have implemented everything I need, so I am not depended on it. Here is a GIST with the code: https://gist.github.com/sscherfke/bf68627762d0b71843e7193c0b1654f8 |
IMO, all those functions, except for |
I think it would be useful to document these in a uuid7 recipe section, then. |
Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
Feature or enhancement
Proposal:
Support for UUIDv7 via
uuid7()
has just landed inmain
: #89083One use-case for UUIDv7 is using it as PK in databases. Since it is time based, it can also be used as partition key (e.g., to use one partion for each day). In order to calculate the partition range, you need calculate the "minimal" UUID for a given date (i.e.,
2025-04-05 00:00:00
and use all zeros for the random bits =>0196033f-4400-7000-8000-000000000000
).I'm totally fine with
uuid.uuid7()
not taking any arguments, but it would be cool if the building blocks for generating a UUIDv7 based on customunix_ts_ms
,counter
, andtail
could be exposed as well.Another useful addition might be a helper that recovers the original datetime/timestamp from a UUIDv7. I understand that this is additional code that might be slightly out of context, but such functions - like
uuid7()
- would probably not need to be changed, but are not trivial to implement for "normal users".These functions could look like this:
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: