diff options
author | Masahiko Sawada <msawada@postgresql.org> | 2024-12-11 15:54:41 -0800 |
---|---|---|
committer | Masahiko Sawada <msawada@postgresql.org> | 2024-12-11 15:54:41 -0800 |
commit | 78c5e141e9c139fc2ff36a220334e4aa25e1b0eb (patch) | |
tree | 0caf5f11b29387097d30a1964ab19abe0ba3ab3b /doc/src | |
parent | 89988ac5891b3d41725472a65e50ae4e192313aa (diff) | |
download | postgresql-78c5e141e9c139fc2ff36a220334e4aa25e1b0eb.tar.gz postgresql-78c5e141e9c139fc2ff36a220334e4aa25e1b0eb.zip |
Add UUID version 7 generation function.
This commit introduces the uuidv7() SQL function, which generates UUID
version 7 as specified in RFC 9652. UUIDv7 combines a Unix timestamp
in milliseconds and random bits, offering both uniqueness and
sortability.
In our implementation, the 12-bit sub-millisecond timestamp fraction
is stored immediately after the timestamp, in the space referred to as
"rand_a" in the RFC. This ensures additional monotonicity within a
millisecond. The rand_a bits also function as a counter. We select a
sub-millisecond timestamp so that it monotonically increases for
generated UUIDs within the same backend, even when the system clock
goes backward or when generating UUIDs at very high
frequency. Therefore, the monotonicity of generated UUIDs is ensured
within the same backend.
This commit also expands the uuid_extract_timestamp() function to
support UUID version 7.
Additionally, an alias uuidv4() is added for the existing
gen_random_uuid() SQL function to maintain consistency.
Bump catalog version.
Author: Andrey Borodin
Reviewed-by: Sergey Prokhorenko, Przemysław Sztoch, Nikolay Samokhvalov
Reviewed-by: Peter Eisentraut, Jelte Fennema-Nio, Aleksander Alekseev
Reviewed-by: Masahiko Sawada, Lukas Fittl, Michael Paquier, Japin Li
Reviewed-by: Marcos Pegoraro, Junwang Zhao, Stepan Neretin
Reviewed-by: Daniel Vérité
Discussion: https://postgr.es/m/CAAhFRxitJv%3DyoGnXUgeLB_O%2BM7J2BJAmb5jqAT9gZ3bij3uLDA%40mail.gmail.com
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/datatype.sgml | 2 | ||||
-rw-r--r-- | doc/src/sgml/func.sgml | 30 |
2 files changed, 24 insertions, 8 deletions
diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index e0d33f12e1c..3e6751d64cc 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -4380,7 +4380,7 @@ SELECT to_tsvector( 'postgraduate' ), to_tsquery( 'postgres:*' ); <para> The data type <type>uuid</type> stores Universally Unique Identifiers - (UUID) as defined by <ulink url="https://datatracker.ietf.org/doc/html/rfc4122">RFC 4122</ulink>, + (UUID) as defined by <ulink url="https://datatracker.ietf.org/doc/html/rfc9562">RFC 9562</ulink>, ISO/IEC 9834-8:2005, and related standards. (Some systems refer to this data type as a globally unique identifier, or GUID,<indexterm><primary>GUID</primary></indexterm> instead.) This diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 2c35252dc06..47370e581ae 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -14256,6 +14256,14 @@ CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple </indexterm> <indexterm> + <primary>uuidv4</primary> + </indexterm> + + <indexterm> + <primary>uuidv7</primary> + </indexterm> + + <indexterm> <primary>uuid_extract_timestamp</primary> </indexterm> @@ -14264,12 +14272,19 @@ CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple </indexterm> <para> - <productname>PostgreSQL</productname> includes one function to generate a UUID: + <productname>PostgreSQL</productname> includes several functions to generate a UUID. <synopsis> <function>gen_random_uuid</function> () <returnvalue>uuid</returnvalue> +<function>uuidv4</function> () <returnvalue>uuid</returnvalue> +</synopsis> + These functions return a version 4 (random) UUID. +<synopsis> +<function>uuidv7</function> (<optional> <parameter>shift</parameter> <type>interval</type> </optional>) <returnvalue>uuid</returnvalue> </synopsis> - This function returns a version 4 (random) UUID. This is the most commonly - used type of UUID and is appropriate for most applications. + This function returns a version 7 UUID (UNIX timestamp with millisecond + precision + sub-millisecond timestamp + random). This function can accept + optional <parameter>shift</parameter> parameter of type <type>interval</type> + which shift internal timestamp by the given interval. </para> <para> @@ -14283,9 +14298,10 @@ CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple <function>uuid_extract_timestamp</function> (uuid) <returnvalue>timestamp with time zone</returnvalue> </synopsis> This function extracts a <type>timestamp with time zone</type> from UUID - version 1. For other versions, this function returns null. Note that the - extracted timestamp is not necessarily exactly equal to the time the UUID - was generated; this depends on the implementation that generated the UUID. + version 1 and 7. For other versions, this function returns null. Note that + the extracted timestamp is not necessarily exactly equal to the time the + UUID was generated; this depends on the implementation that generated the + UUID. </para> <para> @@ -14293,7 +14309,7 @@ CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple <function>uuid_extract_version</function> (uuid) <returnvalue>smallint</returnvalue> </synopsis> This function extracts the version from a UUID of the variant described by - <ulink url="https://datatracker.ietf.org/doc/html/rfc4122">RFC 4122</ulink>. For + <ulink url="https://datatracker.ietf.org/doc/html/rfc9562">RFC 9562</ulink>. For other variants, this function returns null. For example, for a UUID generated by <function>gen_random_uuid</function>, this function will return 4. |