intro
Let’s learn everything you need to know about the new UUIDv7 features in PostgreSQL 18.
UUIDs represent a popular way to handle unique identifiers, but version 4 (the latest one previously supported by PostgreSQL) has notable limitations, including poor indexing performance and the inability to produce meaningful sorting. UUIDv7 addresses both issues by being time-ordered, which improves index efficiency and enables chronological sorting.
In this article, you will learn everything you need to know about UUIDv7 support in PostgreSQL 18. Discover how it brings a breath of fresh air to ID management!
Understanding the Role of UUIDs in Databases
Relational databases have traditionally used auto-incrementing data types (such as SERIAL
, in Postgres) to generate unique IDs. This mechanism works well on a single machine, but when scaling across multiple nodes, you need a way to generate identifiers that are unique within your application or global environment. That is where UUIDs come in!
According to RFC 4122, a UUID (short for Universally Unique Identifier) is a 128-bit, fixed-size value. In detail, UUIDs are generally used to uniquely identify information in computer systems. In databases like PostgreSQL, UUIDs can serve as primary keys instead of traditional auto-incrementing values.
The main advantage of UUIDs is that they are designed to be unique across both space and time. Plus, they can be generated at high throughput without requiring a centralized authority. The likelihood of two UUIDs being identical is extremely low (approximately 1 in 2¹²², which is about 1 in 5.3×10³⁶).
Transitioning from SERIAL
IDs to UUIDs is a common practice in distributed PostgreSQL deployments. For example, the Instagram engineering team adopted UUIDs when they sharded their PostgreSQL database to ensure global uniqueness across multiple nodes. Learn more in our guide on distributing data via database sharding.
Why Use UUIDs as Primary Keys in Databases?
UUIDs are a strong alternative to auto-incrementing IDs as primary keys for these reasons:
UUID Support in PostgreSQL Before Version 18
Before PostgreSQL 18, the database already supported UUIDs through the dedicated UUID
data type. This type stores a 128-bit value in a compact binary format and is fully compliant with RFC 4122.
In detail, PostgreSQL allows you to generate and work with UUIDs directly in SQL through the following functions:
Additionally, UUIDs can be used natively as primary keys. PostgreSQL supports them efficiently with both B-tree and hash indexes.
The Need for UUIDv7
Over the years, UUIDv4 has become a popular choice for generating globally unique identifiers in PostgreSQL, especially in distributed environments. At the same time, when used as a primary key, it comes with these three serious challenges:
To address these challenges, the new UUIDv7 format was introduced in RFC 9562, published in May 2024.
Compared to UUIDv4, UUIDv7 values are time-based. That means the first 48 bits encode the Unix timestamp, while the remaining bits ensure randomness and uniqueness. This structure makes UUIDv7 sortable by creation time and inserts more index-friendly, resolving the two most critical issues of UUIDv4.
Aspect | UUIDv4 | UUIDv7 |
---|---|---|
Uniqueness | Globally unique | Globally unique |
Size | 128 bits | 128 bits |
Generation method | Random-based | Time-based (Unix timestamp + random) |
Sortability | Not sortable | Sortable by creation time |
Index locality | Poor (random distribution) | Good (sequential insert order) |
Performance Impact | Can cause index fragmentation | Reduced fragmentation, better write performance |
Use case | General-purpose unique IDs | Optimized for databases and distributed systems requiring sortable keys |
Example | 7f9c3608-b686-47f9-81a0-8366cbe81970 | 01987fa9-59f4-752b-98bf-cdae2f2b5f44 |
To experiment with UUIDv7 and learn more about its benefits, visit the UUIDv7 site.
UUIDv7 Introduction in PostgreSQL 18
PostgreSQL 18, currently in Beta 2 and expected to be released by the end of the year, introduces the new uuidv7()
SQL function. This function generates UUID version 7 identifiers, following the specification defined in RFC 9562.
As explained in the official commit, UUIDv7 combines a Unix timestamp in milliseconds with random bits, providing both global uniqueness and time-based sortability. In PostgreSQL’s implementation, a 12-bit sub-millisecond timestamp fraction is stored immediately after the main timestamp.
That fraction acts as a counter that ensures monotonicity within the same millisecond. This detail helps maintain an increasing order of generated UUIDs even if the system clock moves backward or when many UUIDs are created very quickly in the same backend process. In other words, it guarantees that UUIDv7 values generated by the same backend are strictly increasing.
On top of that, PostgreSQL 18 updates the uuid_extract_timestamp()
function to support UUID version 7. For consistency, an alias uuidv4()
is also added for the existing gen_random_uuid()
function.
PostgreSQL UUIDv7 in Action
See how the new PostgreSQL UUIDv7 mechanism works with some query examples:
1
SELECT uuidv7();
The above function will return a UUIDv7 value, such as:
1
'01987fb8-b258-70fd-a574-1c3f9b89ee21'
You can verify that this is a valid UUIDv7 using tools like UUIDGen Validator:

The value appears to be a valid UUIDv7 value as expected.
To confirm the version directly in PostgreSQL, use the uuid_extract_version()
function:
1
SELECT uuid_extract_version('01987fb8-b258-70fd-a574-1c3f9b89ee21'::uuid);
The result will be:
1
7
As you can verify in a full-featured PostgreSQL database client like DbVisualizer:

Similarly, check the result on the new uuidv4()
alias:
1
SELECT uuid_extract_version(uuidv4());
You will get, as you could expect, the following result:
1
4
Then, given a UUIDv7, you can also retrieve its timestamp with uuid_extract_timestamp()
:
1
SELECT uuid_extract_timestamp('01987fb8-b258-70fd-a574-1c3f9b89ee21'::uuid);
The result will be:
1
2025-08-06 14:11:07.480+00
Et voilà! You now know how to generate, inspect, and use UUIDv7 values in PostgreSQL.
Conclusion
In this blog post, you discovered why UUIDs are an indispensable mechanism for PostgreSQL databases, especially in shared or distributed environments. You explored the pros and cons of using UUIDs to identify rows, and saw how PostgreSQL 18 introduced support for the new UUIDv7 format.
Managing PostgreSQL becomes even easier with a feature-rich visual tool like DbVisualizer. It stays up to date with the latest PostgreSQL features and offers powerful capabilities such as query optimization, drag-and-drop query building, ERD-style schema diagrams, and streamlined import/export operations. Download DbVisualizer for free today!
FAQ
Does PostgreSQL support UUIDv7?
Yes, since PostgreSQL 18. Postgres version 18 introduced support for UUIDv7. This new version of UUID is time-ordered, so that it supports scenarios that require both uniqueness and chronological sorting.
What are the main UUID changes in PostgreSQL 18?
The key changes brought by PostgreSQL 18 when it comes to UUIDs are:
What is the uuidv7 function in PostgreSQL 18?
The PostgreSQL 18 uuidv7()
function follows this syntax:
1
uuidv7([shift interval])
It accepts an optional shift
parameter that allows you to adjust the generated timestamp by the specified time interval
. Then, it generates a UUIDv7 value. The timestamp component of the UUID is based on the Unix epoch time with millisecond precision, combined with sub-millisecond data and random bits for uniqueness.
Is the pgcrypto extension required to call gen_random_uuid()?
No, not anymore. The pgcrypto
extension originally provided the gen_random_uuid()
function. However, that function has been included in PostgreSQL’s core since version 13. As a result, you no longer need to install or enable pgcrypto
to use gen_random_uuid()
. The built-in version is now the standard, making the pgcrypto
version obsolete (it calls the core function with the same name under the hood). So, you can simply call gen_random_uuid()
directly in modern PostgreSQL installations.
Why use a database client?
A visual database client like DbVisualizer simplifies database tasks by making it easy to query, manage, and visualize data. Plus, it stays up to date so that you do not have to worry about missing new features like UUIDv7 in PostgreSQL 18. With a user-friendly interface, it lets you work with tables, explore schema relationships, and craft or debug queries using its powerful built-in SQL editor. Notable features include autocomplete, ER diagrams, and data export tools, helping DbVisualizer stand out. Try the Pro version with a 21-day free trial!