Add UUID version 7 generation function.
authorMasahiko Sawada <msawada@postgresql.org>
Wed, 11 Dec 2024 23:54:41 +0000 (15:54 -0800)
committerMasahiko Sawada <msawada@postgresql.org>
Wed, 11 Dec 2024 23:54:41 +0000 (15:54 -0800)
commit78c5e141e9c139fc2ff36a220334e4aa25e1b0eb
tree0caf5f11b29387097d30a1964ab19abe0ba3ab3b
parent89988ac5891b3d41725472a65e50ae4e192313aa
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
doc/src/sgml/datatype.sgml
doc/src/sgml/func.sgml
src/backend/utils/adt/uuid.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.dat
src/test/regress/expected/uuid.out
src/test/regress/sql/uuid.sql