diff --git a/pkg/sql/logictest/testdata/logic_test/alter_primary_key b/pkg/sql/logictest/testdata/logic_test/alter_primary_key index 3125f42d4b21..fb4f94154be9 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_primary_key +++ b/pkg/sql/logictest/testdata/logic_test/alter_primary_key @@ -4,6 +4,9 @@ CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, z INT NOT NULL, w INT, INDEX statement ok INSERT INTO t VALUES (1, 2, 3, 4), (5, 6, 7, 8) +statement error pgcode 0A000 .* contains duplicate column \"y\" +ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y, y) + statement ok ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y, z) diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go index 9fc2023cd349..b50f0f304184 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_primary_key.go @@ -206,6 +206,7 @@ func checkForEarlyExit(b BuildCtx, tbl *scpb.Table, t alterPrimaryKeySpec) { panic(err) } + usedColumns := make(map[tree.Name]bool, len(t.Columns)) for _, col := range t.Columns { if col.Column == "" && col.Expr != nil { panic(errors.WithHint( @@ -217,6 +218,11 @@ func checkForEarlyExit(b BuildCtx, tbl *scpb.Table, t alterPrimaryKeySpec) { "use columns instead", )) } + if usedColumns[col.Column] { + panic(pgerror.Newf(pgcode.FeatureNotSupported, + "new primary key contains duplicate column %q", col.Column)) + } + usedColumns[col.Column] = true colElems := b.ResolveColumn(tbl.TableID, col.Column, ResolveParams{ IsExistenceOptional: false,