cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
29.87k stars 3.77k forks source link

schemachanger: hash-sharded PK DDL results in missing key columns #128420

Open annrpom opened 1 month ago

annrpom commented 1 month ago
demo@127.0.0.1:26257/demoapp/movr> CREATE TABLE public.table_w1_14 (i int not null);                                                                                   
CREATE TABLE

Time: 4ms total (execution 4ms / network 0ms)

Time: 175ms total (execution 174ms / network 0ms)

demo@127.0.0.1:26257/demoapp/movr> ALTER TABLE public.table_w1_14 ADD COLUMN col_w1_14_w1_122 BOX2D NULL UNIQUE, ALTER       
                                -> PRIMARY KEY USING COLUMNS (i) USING HASH;                                   
ERROR: internal error: building declarative schema change targets for ALTER TABLE: programming error: cannot find any KEY index columns in index 2 from table 113
SQLSTATE: XX000
DETAIL: stack trace:
...

This is coming from getting the IndexColumn elements in https://github.com/cockroachdb/cockroach/blob/3225b9a532e1a6650651fd548df88cb491b67178/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go#L584-L587

called from shouldCreateUniqueIndexOnOldPrimaryKeyColumns.

Not entirely sure what is going on, but we intentionally call on this code with the ID of the new index for the PK and expect it to succeed so either we should readjust our expectations or the order of how we add IndexColumns to the builder is off for this case

Jira issue: CRDB-41003

Epic CRDB-40419

rafiss commented 1 month ago

Let's see if the fix for https://github.com/cockroachdb/cockroach/issues/128457 affects this behavior.