Skip to content

opt: eliminate uniqueness checks when uniqueness ensured through randomness #57790

@nvb

Description

@nvb

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 performed

We 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-multiregionRelated to multi-regionA-sql-optimizerSQL logical planning and optimizations.C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)C-performancePerf of queries or internals. Solution not expected to change functional behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions