CockroachDB Unordered Row ID

Viewed 34

Why does CockroachDB add a rowid column to my tables? They are INT values and do not look ordered, does this column give up the sort order and/or impact how range scans work?

1 Answers

CockroachDB automatically adds a rowid column that serves as a primary key if no primary key is specified for the table. rowid values are generated as a combination of the insert timestamp and the ID of the node executing the statement, as such, ordering is maintained.

To create your own rowid, two functions are commonly used:

  • unique_rowid(): ensures a unique integer for a primary key, value always increases
  • unordered_unique_rowid(): ensures a unique integer for a primary key but the rowid value does not always increase. Having rowid values that do not always increase helps divide the key-space more evenly, preventing range hotspots.

Helpful docs from CockroachDB:
Create a table
Auto-generate unique row ids
ID generation functions

Helpful Blog Post:
CockroachDB Key Generation Part 3 - Unordered RowID

Related