-
Notifications
You must be signed in to change notification settings - Fork 20
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
Would you accept a PR to generate a ULID in UUID format? #3
Comments
Why not just: RETURN encode( timestamp || gen_random_bytes(10) ,'hex')::uuid; Doing a benchmark shows its a bit faster |
Because I'm absolute trash at SQL @KnowZero :) Thanks for the tweak. On a different note, here's a function to extract the epoch milliseconds out of a UUID formatted ULID:
(This uses undocumented behavior as described here.) In most cases it isn't appropriate to extract the datetime out of a ULID, but I found a use case in my own domain. |
What is the performance like on this compared with uuid generation? |
It would be handy to have a possibility of passing custom timestamp. Then I wouldn't need composite unique index or a primary key like (id, timestamp). Timestamp could then be extracted from ULID, used for query, conditions or even for partitions... |
I wrote the following, heavily inspired by what you already wrote:
It generates a ULID, but in the UUID format. The order of the bytes is a bit unintuitive due to the UUID format/endianness.
This function makes it easier to use the
uuid
PostGres type to hold ULIDs. This makes for smaller, faster indexes (c.f. indexing ontext
).The text was updated successfully, but these errors were encountered: