cockroachdb / cockroachdb/cockroach

When enabled for a table, region inference should be the default for that table

Open
#150,783 2 comments 1 reaction 0 assignees View on GitHub
A-sql-fks branch-release-25.3 C-bug docs-known-limitation O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Given the following schema:

```
-- Parent table - REGIONAL BY ROW automatically adds crdb_region column
CREATE TABLE parent (
id INT PRIMARY KEY,
data TEXT
) LOCALITY REGIONAL BY ROW;

-- Child table with region inference via foreign key constraint
CREATE TABLE child (
id INT PRIMARY KEY,
parent_id INT,
data TEXT,
CONSTRAINT fk_parent FOREIGN KEY (crdb_region, parent_id) REFERENCES parent (crdb_region, id)
) WITH (infer_rbr_region_col_using_constraint = 'fk_parent') LOCALITY REGIONAL BY ROW;
```

with the following data:

```
demo@127.0.0.1:26257/race_test> select *, crdb_region from parent;
id | data | crdb_region
-----+------+--------------
1 | data | us-east1
(1 row)
```

Executing the following statement from us-west1:

```
demo@localhost:26260/race_test> insert into child(id, parent_id, data, crdb_region) values (2, 1, 'foo', DEFAULT);
ERROR: insert on table "child" violates foreign key constraint "fk_parent"
SQLSTATE: 23503
DETAIL: Key (crdb_region, parent_id)=('us-west1', 1) is not present in table "parent".
CONSTRAINT: fk_parent
```

This is a bit surprising based on the DDL I provided.

Looking at the table definition, the reason is clear:

```
demo@127.0.0.1:26257/race_test> show create table child;
table_name | create_statement
-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------
child | CREATE TABLE public.child (
| id INT8 NOT NULL,
| parent_id INT8 NULL,
| data STRING NULL,
| crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
| CONSTRAINT child_pkey PRIMARY KEY (id ASC),
| CONSTRAINT fk_parent FOREIGN KEY (crdb_region, parent_id) REFERENCES public.parent(crdb_region, id)
| ) WITH (infer_rbr_region_col_using_constraint = "fk_parent") LOCALITY REGIONAL BY ROW;
(1 row)

Time: 929ms total (execution 928ms / network 1ms)
```

But maybe we should change the default value to be the inference?

This was testing on 25.3.0-rc0

Jira issue: CRDB-52906

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.