-
Notifications
You must be signed in to change notification settings - Fork 4.1k
opt: eliminate uniqueness checks when uniqueness ensured through randomness #57790
Description
By default, a REGIONAL BY ROW table will contain a unique primary, which will require a fanout query on INSERT to validate uniqueness. This is possible using partitioned unique indexes and is desirable in the most general case to preserve schema semantics. But it also causes INSERTs to REGIONAL BY ROW tables to be slow, which undermines a major reason why people would want to use the table type in the first place.
We can optimize this in the case where global uniqueness can be enforced through the use of randomness.
For v21.1, we will omit the uniqueness checks for UNIQUE WITHOUT INDEX constraints if any of the columns in the unique key are generated on insertion by CockroachDB through an expression that uses the gen_random_uuid() builtin.
For example:
CREATE TABLE t (id UUID PRIMARY KEY DEFAULT gen_random_uuid()) REGIONAL BY ROW;
INSERT INTO t VALUES (DEFAULT); -- global uniqueness check omitted
INSERT INTO t VALUES (gen_random_uuid()); -- global uniqueness check omitted
INSERT INTO t VALUES ('dd5774fd-7f24-4821-9e14-5ba393d8cf81'); -- global uniqueness check performedWe will warn that this is relying on the extremely low but non-zero chance of UUID collision in the documentation.
We will also provide users with a way to opt-out of this behavior through a cluster setting.
Epic CRDB-2528