Skip to main content
  • To bulk-insert data into an existing table, batch multiple rows in one statement and do not include the INSERT statements within a transaction. Experimentally determine the optimal batch size for your application by monitoring the performance for different batch sizes (10 rows, 100 rows, 1000 rows).
You can also use the statement to bulk-insert CSV data into an existing table.
To auto-generate unique row identifiers, you can use the following :
  • Use gen_random_uuid(): Generates a UUIDv4 with UUID data type.
  • Use uuid_v4(): Generates a UUIDv4 with BYTES data type.
  • Use unique_rowid(): Generates a globally unique INT data type 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 .

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?. Sequential numbers can be generated in CockroachDB using the unique_rowid() built-in function or using . However, note the following considerations:
  • Unless you need roughly-ordered numbers, use values instead. See the previous FAQ for details.
  • produce unique values. However, not all values are guaranteed to be produced (e.g., when a transaction is canceled after it consumes a value) and the values may be slightly reordered (e.g., when a transaction that consumes a lower sequence number commits after a transaction that consumes a higher number).
  • For maximum performance, avoid using sequences or unique_rowid() to generate row IDs or indexed columns. Values generated in these ways are logically close to each other and can cause on a few data ranges during inserts. Instead, prefer identifiers.
  • We . If a table must be indexed on sequential keys, use . Hash-sharded indexes distribute sequential traffic uniformly across ranges, eliminating single-range and improving write performance on sequentially-keyed indexes at a small cost to read performance.
Property generated with uuid\_v4()INT generated with unique\_rowid()Sequences
Size16 bytes8 bytes1 to 8 bytes
Ordering propertiesUnorderedHighly time-orderedHighly time-ordered
Performance cost at generationSmall, scalableSmall, scalableVariable, can cause
Value distributionUniformly distributed (128 bits)Contains time and space (node ID) componentsDense, small values
Data localityMaximally distributedValues generated close in time are co-locatedHighly local
INSERT latency when used as keySmall, insensitive to concurrencySmall, but increases with concurrent INSERTsHigher
INSERT throughput when used as keyHighestLimited by max throughput on 1 nodeLimited by max throughput on 1 node
Read throughput when used as keyHighest (maximal parallelism)LimitedLimited
Most use cases that ask for a strong time-based write ordering can be solved with other, more distribution-friendly solutions instead. For example, CockroachDB’s time travel queries (AS OF SYSTEM TIME) support the following:
  • Paginating through all the changes to a table or dataset
  • Determining the order of changes to data over time
  • Determining the state of data at some point in the past
  • Determining the changes to data between two points of time
Consider also that the values generated by unique_rowid(), described in the previous FAQ entries, also provide an approximate time ordering. However, if your application absolutely requires strong time-based write ordering, it is possible to create a strictly monotonic counter in CockroachDB that increases over time as follows:
  • Initially: CREATE TABLE cnt(val INT PRIMARY KEY); INSERT INTO cnt(val) VALUES(1);
  • In each transaction: INSERT INTO cnt(val) SELECT max(val)+1 FROM cnt RETURNING val;
This will cause transactions to conflict with each other and effectively force the transactions to commit one at a time throughout the cluster, which in turn guarantees the values generated in this way are strictly increasing over time without gaps. The caveat is that performance is severely limited as a result. If you find yourself interested in this problem, please and describe your situation. We would be glad to help you find alternative solutions and possibly extend CockroachDB to better match your needs. There’s no function in CockroachDB for returning last inserted values, but you can use the of the INSERT statement. For example, this is how you’d use RETURNING to return a value auto-generated via unique_rowid() or :
Transaction contention occurs when transactions issued from multiple clients at the same time operate on the same data. This can cause transactions to wait on each other and decrease performance, like when many people try to check out with the same cashier at a store. For more information about contention, see . . Yes, the data type is supported. To see which indexes CockroachDB is using for a given query, you can use the statement, which will print out the query plan, including any indexes that are being used:
If you’d like to tell the query planner which index to use, you can do so via some :
You can enable the CockroachDB that record SQL events. Yes. For more details, see . When an clause is not used in a query, rows are processed or returned in a non-deterministic order. “Non-deterministic” means that the actual order can depend on the logical plan, the order of data on disk, the topology of the CockroachDB cluster, and is generally variable over time. In CockroachDB, all INTs are represented with 64 bits of precision, but JavaScript numbers only have 53 bits of precision. This means that large integers stored in CockroachDB are not exactly representable as JavaScript numbers. For example, JavaScript will round the integer 235191684988928001 to the nearest representable value, 235191684988928000. Notice that the last digit is different. This is particularly problematic when using the unique_rowid() , since unique_rowid() nearly always returns integers that require more than 53 bits of precision to represent. To avoid this loss of precision, Node’s pg driver will, by default, return all CockroachDB INTs as strings.
To perform another query using the value of idString, you can simply use idString directly, even where an INT type is expected. The string will automatically be coerced into a CockroachDB INT.
If you instead need to perform arithmetic on INTs in JavaScript, you will need to use a big integer library like Long.js. Do not use the built-in parseInt function.
CockroachDB is a distributed SQL database built on a transactional and strongly-consistent key-value store. Although it is not possible to access the key-value store directly, you can mirror direct access using a “simple” table of two columns, with one set as the primary key:
When such a “simple” table has no indexes or foreign keys, /// statements translate to key-value operations with minimal overhead (single digit percent slowdowns). For example, the following UPSERT to add or replace a row in the table would translate into a single key-value Put operation:
This SQL table approach also offers you a well-defined query language, a known transaction model, and the flexibility to add more columns to the table if the need arises. Yes. For more information, see . Depending on your use case, you may prefer to use to do fuzzy string matching and pattern matching. For more information about use cases for trigram indexes that could make having full-text search unnecessary, see the 2022 blog post Use cases for trigram indexes (when not to use Full Text Search).

See also