9.14. Функции генерирования UUID #

В Postgres Pro имеется функция для генерирования UUID:

gen_random_uuid () → uuid

Она возвращает случайный UUID версии 4. Это наиболее популярный тип UUID, подходящий для большинства приложений.

Модуль uuid-ossp предоставляет дополнительные функции, реализующие другие стандартные алгоритмы генерирования UUID.

Также есть функции для извлечения данных из UUID:

uuid_extract_timestamp (uuid) → timestamp with time zone

Эта функция извлекает timestamp with time zone из UUID версии 1. Для других версий эта функция возвращает NULL. Обратите внимание, что извлечённая метка времени не обязательно точно равна времени генерации UUID, это зависит от реализации, сгенерировашей UUID.

uuid_extract_version (uuid) → smallint

Эта функция извлекает версию из UUID варианта, описанного в RFC 4122. Для других вариантов эта функция возвращает NULL. Например, для UUID, сгенерированного gen_random_uuid, эта функция вернёт 4.

В Postgres Pro также реализованы показанные в Таблице 9.1 операторы для сравнения значений UUID.

9.14. UUID Functions #

Postgres Pro includes one function to generate a UUID:

gen_random_uuid () → uuid

This function returns a version 4 (random) UUID. This is the most commonly used type of UUID and is appropriate for most applications.

The uuid-ossp module provides additional functions that implement other standard algorithms for generating UUIDs.

There are also functions to extract data from UUIDs:

uuid_extract_timestamp (uuid) → timestamp with time zone

This function extracts a timestamp with time zone 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.

uuid_extract_version (uuid) → smallint

This function extracts the version from a UUID of the variant described by RFC 4122. For other variants, this function returns null. For example, for a UUID generated by gen_random_uuid, this function will return 4.

Postgres Pro also provides the usual comparison operators shown in Table 9.1 for UUIDs.

FAQ