Skip to main content
The UUID (Universally Unique Identifier) implements the UUIDv4 format from RFC 4122. It stores a 128-bit value that is “unique across both space and time, with respect to the space of all UUIDs” as specified by the RFC. To auto-generate UUIDs:
  • Use the gen_random_uuid() function as the default value of the UUID data type.
  • Use the uuid_v4() function as the default value of the data type.
For examples, refer to Create a table with auto-generated unique row IDs. For performance reasons, if you are going to use UUIDs, Cockroach Labs strongly recommends using UUIDv4 as defined by RFC 4122. This is the format generated by the . Other types of UUID are largely untested with CockroachDB and will require performance testing to avoid hotspots.

Syntax

You can express UUID values using the following formats:
FormatDescription
Standard RFC4122 (UUIDv4) formatHyphen-separated groups of 8, 4, 4, 4, and 12 hexadecimal digits.

Example: acde070d-8c4c-4f0d-9d8a-162843c10333
BYTESUUID value specified as a value.

Example: b'kafef00ddeadbeed'
Uniform Resource NameA Uniform Resource Name (URN) specified as “urn:uuid:” followed by the RFC4122 format.

Example: urn:uuid:63616665-6630-3064-6465-616462656564
Alternate PostgreSQL-supported formatsAll alternate UUID formats supported by PostgreSQL, including the RFC4122 format surrounded by braces, any supported format with upper-case digits, any supported format with some or all hyphens omitted, and any supported format with hyphens after any group of four digits.

Examples: , ACDE070D-8C4C-4f0D-9d8A-162843c10333, acde070d8c4c4f0d9d8a162843c10333, acde-070d-8c4c-4f0d-9d8a-1628-43c1-0333
CockroachDB displays all UUID values in the standard RFC4122 format, and implements the UUIDv4 (random) version from the RFC.

Size

A UUID value is 128 bits in width, but the total storage size is likely to be larger due to CockroachDB metadata.

Examples

Create a table with manually-entered UUID values

For performance reasons, if you are going to use UUIDs, Cockroach Labs strongly recommends using UUIDv4 as defined by RFC 4122. This is the format generated by the . Other types of UUID are largely untested with CockroachDB and will require performance testing to avoid hotspots.

Create a table with UUID in standard RFC4122-specified format

Create a table with UUID in BYTE format

Create a table with UUID used as URN

Express UUIDs in alternate formats

Create a table with auto-generated unique row IDs

To auto-generate unique row identifiers, you can use the following : For performance reasons, if you are going to use UUIDs, Cockroach Labs strongly recommends using UUIDv4 as defined by RFC 4122. This is the format generated by the . Other types of UUID are largely untested with CockroachDB and will require performance testing to avoid hotspots.

Use gen_random_uuid()

To use the column with the gen_random_uuid() as the :

Use uuid_v4()

Alternatively, you can use the column with the uuid_v4() function as the default value:
In either case, generated IDs will be 128-bit, sufficiently large to generate unique values. Once the table grows beyond a single key-value range’s , new IDs will be scattered across all of the table’s ranges and, therefore, likely across different nodes. This means that multiple nodes will share in the load. This approach has the disadvantage of creating a primary key that may not be useful in a query directly, which can require a join with another table or a secondary index.

Use unique_rowid()

If it is important for generated IDs to be stored in the same key-value range, you can use an with the unique_rowid() as the default value, either explicitly or via the :
Upon insert or upsert, the unique_rowid() function generates a default value from the timestamp and ID of the node executing the insert. Such time-ordered values are likely to be globally unique except in cases where a very large number of IDs (100,000+) are generated per node per second. Also, there can be gaps and the order is not completely guaranteed. To understand the differences between the UUID and unique_rowid() options, see the . For further background on UUIDs, see What is a UUID, and Why Should You Care?.

Supported casting and conversion

UUID values can be to the following data type:
TypeDetails
BYTESRequires supported string format, e.g., b'\141\061\142\062\143\063'.

See also