cockroachdb / cockroachdb/cockroach

sql: concurrent inserts with ADD COLUMN UNIQUE do not work on RBR tables

Open
#146,360 9 comments 0 reactions 0 assignees View on GitHub
branch-master branch-release-24.1 branch-release-24.3 branch-release-25.1 branch-release-25.2 C-bug S-2 T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Attempting to insert on a regional by row table with concurrent ADD COLUMN unique runs into an error inside the optbuilder.

On master we see following stack on the insert:
```
ERROR: internal error: runtime error: index out of range [-1]
SQLSTATE: XX000
DETAIL: stack trace:
pkg/util/errorutil/catch.go:24: ShouldCatch()
pkg/sql/opt/optbuilder/builder.go:247: func1()
GOROOT/src/runtime/panic.go:791: gopanic()
GOROOT/src/runtime/panic.go:115: goPanicIndex()
pkg/sql/opt/metadata.go:937: ColumnMeta()
pkg/sql/opt/optbuilder/mutation_builder_unique.go:310: init()
pkg/sql/opt/optbuilder/mutation_builder_unique.go:71: buildUniqueChecksForInsert()
pkg/sql/opt/optbuilder/insert.go:792: buildInsert()
pkg/sql/opt/optbuilder/insert.go:313: buildInsert()
pkg/sql/opt/optbuilder/builder.go:395: func2()
pkg/sql/opt/optbuilder/with.go:110: processWiths()
pkg/sql/opt/optbuilder/builder.go:394: buildStmt()
pkg/sql/opt/optbuilder/builder.go:321: buildStmtAtRootWithScope()
pkg/sql/opt/optbuilder/builder.go:302: buildStmtAtRoot()
pkg/sql/opt/optbuilder/builder.go:281: Build()
pkg/sql/plan_opt.go:854: buildExecMemo()
pkg/sql/plan_opt.go:261: makeOptimizerPlan()
pkg/sql/conn_executor_exec.go:3308: makeExecPlan()
pkg/sql/conn_executor_exec.go:2855: dispatchToExecutionEngine()
pkg/sql/conn_executor_exec.go:1080: execStmtInOpenState()
pkg/sql/conn_executor_exec.go:172: func2()
pkg/sql/conn_executor_exec.go:4477: execWithProfiling()
pkg/sql/conn_executor_exec.go:171: execStmt()
pkg/sql/conn_executor.go:2342: func1()
pkg/sql/conn_executor.go:2347: execCmd()
pkg/sql/conn_executor.go:2264: run()
pkg/sql/conn_executor.go:1048: ServeConn()
pkg/sql/pgwire/conn.go:252: processCommands()
pkg/sql/pgwire/server.go:1197: func4()
src/runtime/asm_arm64.s:1223: goexit()

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

This problem is easily reproducible in the legacy and declarative schema changer (25.2, and master only). Start demo with `cockroach demo --geo-partitioned-replicas`, then execute:
```
SET use_declarative_schema_changer=off;
CREATE DATABASE multiregion_db PRIMARY REGION "us-east1" REGIONS "us-west1", "europe-west1" SURVIVE REGION FAILURE;
CREATE TABLE multiregion_db.public.table_regional_by_row (
k INT PRIMARY KEY,
V STRING
) LOCALITY REGIONAL BY ROW;

SET CLUSTER SETTING jobs.debug.pausepoints = 'schemachanger.before.exec';
ALTER TABLE multiregion_db.public.table_regional_by_row ADD COLUMN j INT DEFAULT unique_rowid() UNIQUE;
INSERT INTO multiregion_db.public.table_regional_by_row values(5);
```

with declarative:
```
CREATE DATABASE multiregion_db PRIMARY REGION "us-east1" REGIONS "us-west1", "europe-west1" SURVIVE REGION FAILURE;
CREATE TABLE multiregion_db.public.table_regional_by_row (
k INT PRIMARY KEY,
V STRING
) LOCALITY REGIONAL BY ROW;

SET CLUSTER SETTING jobs.debug.pausepoints = 'schemachanger.demo.$ cockroach demo.9';
ALTER TABLE multiregion_db.public.table_regional_by_row ADD COLUMN j INT DEFAULT unique_rowid() UNIQUE;
INSERT INTO multiregion_db.public.table_regional_by_row values(5);
```

**When resolving this issue, please unskip the test case in `pkg/ccl/schemachangerccl/testdata/end_to_end/add_column_multiple_regional_by_row/add_column_multiple_regional_by_row.definition`**

Jira issue: CRDB-50496

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.