cockroachdb / cockroachdb/cockroach

sql: support adding unique indexes including crdb_region on REGIONAL BY ROW tables via ALTER TABLE

Open
#155,252 0 comments 2 reactions 0 assignees View on GitHub
C-enhancement
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Currently, CockroachDB supports creating unique constraints on REGIONAL BY ROW tables that include the implicit crdb_region column at table creation time. However, attempting to add the same constraint after table creation via ALTER TABLE ... ADD CONSTRAINT ... UNIQUE (...) fails with an error about implicit partitioning columns and hash-sharded indexes.

Example:

```
CREATE TABLE transaction (
id UUID,
reference_id TEXT
) LOCALITY REGIONAL BY ROW;

ALTER TABLE transaction
ADD CONSTRAINT transaction_reference_id_unique
UNIQUE (crdb_region, reference_id);
```

Results in:

`ERROR: hash sharded indexes cannot include implicit partitioning columns from "PARTITION ALL BY" or "LOCALITY REGIONAL BY ROW"`

The same constraint, when defined in the CREATE TABLE statement, works correctly:

```
CREATE TABLE transaction (
id UUID,
reference_id TEXT,
UNIQUE (crdb_region, reference_id)
) LOCALITY REGIONAL BY ROW;
```

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

`ALTER TABLE ... ADD CONSTRAINT ... UNIQUE` (crdb_region, reference_id) should succeed on a `REGIONAL BY ROW` table — equivalently to defining the constraint at table creation time — because both paths conceptually produce the same locality-aware index.

Jira issue: CRDB-55339

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the CREATE TABLE and ALTER TABLE examples in the issue and compare how each creates the locality-aware unique index. Trace the SQL handling for ALTER TABLE ... ADD CONSTRAINT ... UNIQUE on REGIONAL BY ROW tables; done when the ALTER form accepts crdb_region and behaves like the equivalent table-creation form.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.