cockroachdb / cockroachdb/cockroach

sql: `Internal error` with FK referencing column with duplicate values on table with `skip_unique_checks`.

Open
#167,122 3 comments 0 reactions 1 assignee Claimed by @DrewKimball View on GitHub
branch-master branch-release-26.2 C-bug O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

In https://github.com/cockroachdb/cockroach/pull/163378 we introduced the `skip_unique_checks` storage parameter.

Claude helped finding this bug during QA:

1. Spin up a MR demo cluster:

```bash
./cockroach demo --nodes=9 --multitenant=false --no-example-database
```

2. Run the following sql script
```sql
CREATE DATABASE db PRIMARY REGION "us-east1" REGIONS "us-west1", "europe-west1";
USE db;

CREATE TABLE parent (
id INT PRIMARY KEY,
val INT,
UNIQUE INDEX idx_val (val)
) LOCALITY REGIONAL BY ROW;

ALTER INDEX parent@idx_val SET (skip_unique_checks = true);

INSERT INTO parent (crdb_region, id, val) VALUES ('us-east1', 1, 100);
INSERT INTO parent (crdb_region, id, val) VALUES ('us-west1', 2, 100);

CREATE TABLE child (
id INT PRIMARY KEY,
parent_val INT REFERENCES parent(val)
) LOCALITY REGIONAL BY ROW;

INSERT INTO child VALUES (1, 100);
```

And we will see the internal error:

```
ERROR: internal error: expected to fetch no more than 1 rows, found 2
SQLSTATE: XX000
DETAIL: stack trace:
pkg/sql/rowexec/joinreader.go:1133: assertBatchRowCounts()
pkg/sql/rowexec/joinreader.go:940: readInput()
pkg/sql/rowexec/joinreader.go:814: Next()
pkg/sql/colexec/columnarizer.go:244: Next()
pkg/sql/colflow/stats.go:122: next()
pkg/sql/colexecerror/error.go:162: CatchVectorizedRuntimeError()
pkg/sql/colflow/stats.go:130: Next()
pkg/sql/colexec/materializer.go:248: next()
pkg/sql/colexec/materializer.go:276: nextAdapter()
pkg/sql/colexecerror/error.go:162: CatchVectorizedRuntimeError()
pkg/sql/colexec/materializer.go:282: Next()
pkg/sql/row_source_to_plan_node.go:80: Next()
pkg/sql/error_if_rows.go:37: Next()
pkg/sql/plan_node_to_row_source.go:223: Next()
pkg/sql/colexec/columnarizer.go:244: Next()
pkg/sql/colflow/stats.go:122: next()
pkg/sql/colexecerror/error.go:162: CatchVectorizedRuntimeError()
pkg/sql/colflow/stats.go:130: Next()
pkg/sql/colflow/flow_coordinator.go:244: nextAdapter()
pkg/sql/colexecerror/error.go:162: CatchVectorizedRuntimeError()
pkg/sql/colflow/flow_coordinator.go:248: next()
pkg/sql/colflow/flow_coordinator.go:302: Run()
pkg/sql/colflow/vectorized_flow.go:317: Run()
pkg/sql/distsql_running.go:1087: Run()
pkg/sql/distsql_running.go:2854: planAndRunPostquery()
pkg/sql/distsql_running.go:2944: func5()
pkg/sql/distsql_running.go:3013: planAndRunChecksInParallel()
pkg/sql/distsql_running.go:2597: PlanAndRunPostQueries()
pkg/sql/distsql_running.go:2130: PlanAndRunAll()
pkg/sql/conn_executor_exec.go:3701: execWithDistSQLEngine()
pkg/sql/conn_executor_exec.go:3185: dispatchToExecutionEngine()
pkg/sql/conn_executor_exec.go:1178: execStmtInOpenState()

HINT: You have encountered an unexpected error.
```

----

Note, timing does matter a lot here, so i wonder if this is also related to lease transfer. If I split the original sql script into 2 parts:

```sql
CREATE DATABASE db PRIMARY REGION "us-east1" REGIONS "us-west1", "europe-west1";
USE db;

CREATE TABLE parent (
id INT PRIMARY KEY,
val INT,
UNIQUE INDEX idx_val (val)
) LOCALITY REGIONAL BY ROW;

ALTER INDEX parent@idx_val SET (skip_unique_checks = true);
```

and `debug2.sql`:

```sql
USE db;
INSERT INTO parent (crdb_region, id, val) VALUES ('us-east1', 1, 100);
INSERT INTO parent (crdb_region, id, val) VALUES ('us-west1', 2, 100);

CREATE TABLE child (
id INT PRIMARY KEY,
parent_val INT REFERENCES parent(val)
) LOCALITY REGIONAL BY ROW;

-- This triggers: internal error: expected to fetch no more than 1 rows, found 2
INSERT INTO child VALUES (1, 100);
```

and run them with:

```bash
kill -9 $(lsof -t -i:26257);
./cockroach demo --nodes=9 --multitenant=false --no-example-database --insecure --background &
sleep 3;
./cockroach sql --insecure --port=26257 --database=defaultdb < ./debug1.sql
sleep 3;
./cockroach sql --insecure --port=26257 --database=defaultdb < ./debug2.sql
```

No error would happen.

Jira issue: CRDB-62257

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.