unique_rowid() function that allows smaller ranges #68505
Labels
C-enhancement
Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
T-sql-foundations
SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
Is your feature request related to a problem? Please describe.
When migrating from a database to CockroachDB, it is common to have table keys that are based on Int4 with id generation done by sequences. When moving to Cockroach, the best practice is to replace these int-based keys with UUIDs since the UUID values create spread in INSERT workloads. However, changing the data types of keys from int to UUID is an expensive operation. A nice compromise is to use the unique_rowid() function to return int-based values which have more spread. unique_rowid() creates values in the int8 range.
However, the maximum safe value that can be passed through JSON (and therefore through most RESTful APIs) is 2^53. When you pass an int8 value (i.e., in the 2^63 range) that exceeds 2^53, Java/JSON truncates digits. A solution to this is to force these values to be passed as strings, but again, this is a painful workaround.
Describe the solution you'd like
I think it would be helpful to have a version of unique_rowid() that produces values in a smaller range. Ideally, this range could be specified through a maxValue parameter, but I think even a hard-coded unique_rowid_guaranteed_to_be_less_javascript_max_value() function would be a big step towards making many migrations eaiser.
Describe alternatives you've considered
Alternative 1: recode the whole app stack to use UUIDs
Alternative 2: record the app stack to convert int8 values to string for transmission through JSON layers
Alternative 3: use an "alternate key" where the table has a PK which is a UUID but also has a unique int4-based id field (with sharded unique index). The PK is used to distribute the primary index, and the ID is used to pass data through the existing API layers.
Additional context
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
The text was updated successfully, but these errors were encountered: