UUID functions
UUID functions generate identifiers and inspect their version or embedded timestamp. Generation returns a native UUID, which can be stored in a UUID column or used as a key.
Version 4 identifiers use random bits. Version 7 identifiers include a timestamp; extraction returns NULL for UUID versions or variants that do not carry a supported timestamp. The generated identifier itself is nondeterministic.
Example: Checking generated UUID versions
SELECT uuid_extract_version(gen_random_uuid()), uuid_extract_version(uuidv7())
FORMAT csv;4,7gen_random_uuid
Generate an RFC 9562 random UUID version 4 from operating-system entropy.
Syntax
gen_random_uuid()Arguments
No arguments. Nondeterministic; independent of setseed.
Returned value
Native UUID version 4; an unavailable entropy source raises IO_ERROR.
Example
SELECT uuid_extract_version(gen_random_uuid()) FORMAT csv;4uuid_extract_timestamp
Extract the UTC timestamp carried by an RFC 9562 UUID version 1 or 7.
Syntax
uuid_extract_timestamp(uuid)Arguments
Native UUID or accepted UUID text. Version 1 drops submicrosecond digits; version 7 exposes its millisecond timestamp. Other versions/variants and SQL NULL return NULL.
Returned value
Native UTC DateTime64(6), or NULL when no supported timestamp is present.
Example
SELECT date_part('year',uuid_extract_timestamp('019535d9-3df7-79fb-b466-fa907fa17f9e')) FORMAT csv;2025.0uuid_extract_version
Extract the version nibble from an RFC 9562 variant UUID.
Syntax
uuid_extract_version(uuid)Arguments
Native UUID or accepted UUID text; SQL NULL propagates. Other UUID variants return NULL.
Returned value
INTEGER version from zero through fifteen, or NULL for a non-RFC variant.
Example
SELECT uuid_extract_version('41db1265-8bc1-4ab3-992f-885799a4af1d') FORMAT csv;4uuidv7
Generate an RFC 9562 time-ordered UUID version 7 with an ascending submillisecond fraction.
Syntax
uuidv7([shift])Arguments
Optional native/text interval or make_interval span shifts UTC time with calendar semantics. SQL NULL propagates. Timestamp must fit unsigned 48-bit Unix milliseconds; random bits use OS entropy.
Returned value
Native UUID version 7. Unshifted calls ascend within one connection; shifted calls follow the supplied interval.
Example
SELECT uuid_extract_version(uuidv7()) FORMAT csv;7