Everyone likes fast Postgres databases, and so do I. Something developers have been talking about recently is the usage of UUID v7 in Postgres databases because rt is quicker to search on. I wanted to use v7 as IDs for the service I built, but I also didn’t want to generate the UUID in the application layer as I think it’s really nice to use default
in SQL. This article shows a quick example of how I implemented it for my services as Postgres don’t support V7 yet.
If you are unfamiliar with the differences between the various UUID versions, I can provide a quick overview:
UUID versions 1, 6, and 7 are generated using a timestamp, monotonic counter, and MAC address. Version 2 is specifically for security IDs. Version 3 is created from MD5 hashes of given data. Version 4 is generated from completely random data. Version 5 is generated from SHA1 hashes of provided data. Version 8 is completely customizable. For most developers, version 4 is sufficient and performs well. However, if you plan to use UUIDs for sorting purposes, you may experience slower sorting queries due to the randomness of the data. In this case, version 7 would be preferred for faster queries.
The SQL function
A disclaimer is that I did not write this function myself. I found it on a Github thread. What the function does is it utilizes the existing gen_random_uuid
function, which is the v4 implementation. We use clock_timestamp
to obtain the current time, extract the epoch time in milliseconds as v7 uses milliseconds, and then convert the millisecond timestamp to a byte sequence using int8send
. To incorporate the timestamp byte sequence into the UUID, we use overlay
to replace the first part of the UUID with the byte sequence. Additionally, we need to add the version of the UUID by changing the 52nd and 53rd bits in the byte array using set_bit
. We simply set both the 52
and 53
bits to 1 to indicate version 7. Finally, we use encode to convert it back to a UUID
.
|
|
Compairing generating uuid V7 and v4
As you may have noticed, it generates a v7 UUID based on v4, which also explains why it is a bit slower at generating a v7.
|
|
Inserting and sorting on UUIDS
The most interesting part is when we use the v7 UUID. So what I did was a super simple test just to see if it’s faster. I used timing
in Postgres to see how long the query takes. I also created 2 new tables with 1 id
column of type UUID and then I inserted 1 million rows into each table with respective UUID versions and queried it with a simple sort.
|
|
With this test, we can see that v7 is 13.44 times faster (42.042 ms).
I also performed a quick EXPLAIN ANALYZE
on v7 and obtained the following results:
|
|
For v4:
|
|
The end
I hope you enjoyed this article. If you have any suggestions for changes, please let me know. You can reach me at X. Have a great day!