Skip to content

Commit

Permalink
sql: ALTER PK carries over comments from old to new primary index
Browse files Browse the repository at this point in the history
Fixes #114081

Release note (bug fix): Previously, when session variable
`use_declarative_schema_changer=off`, ALTER PK would delete any comments
associated with the old primary index and with the old primary key
constraint. This is inconsistent with the behavior with
when `use_declarative_schema_changer=on`, which is the default setting,
where those comments would be carried over to the new primary index.
Furthermore, the old behavior also caused a bug that could prevent
command `SHOW CREATE t` from working (see #114081).
  • Loading branch information
Xiang-Gu committed Nov 14, 2023
1 parent d7d1479 commit 891b356
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
15 changes: 14 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,7 @@ statement ok
COMMENT ON INDEX pkey IS 'idx';
COMMENT ON CONSTRAINT pkey ON pkey_comment IS 'const';

# Create a unique secondary index and add a comment on it.
statement ok
CREATE UNIQUE INDEX i2 ON pkey_comment(c);

Expand All @@ -1592,7 +1593,9 @@ COMMENT ON CONSTRAINT i2 ON public.pkey_comment IS 'idx3'
statement ok
ALTER TABLE pkey_comment ALTER PRIMARY KEY USING COLUMNS (b);

# No comment exists inside the CREATE statement
# Verify that all comments are appropriately carried over. The output of `SHOW
# CREATE` between LSC and DSC are slightly different so we query them
# separately.
skipif config local-legacy-schema-changer
query T
SELECT substring(create_statement, strpos(create_statement, 'COMMENT')) FROM [SHOW CREATE pkey_comment];
Expand All @@ -1602,6 +1605,16 @@ COMMENT ON INDEX public.pkey_comment@pkey IS 'idx';
COMMENT ON CONSTRAINT i2 ON public.pkey_comment IS 'idx3';
COMMENT ON CONSTRAINT pkey ON public.pkey_comment IS 'const'

onlyif config local-legacy-schema-changer
query T
SELECT substring(create_statement, strpos(create_statement, 'COMMENT')) FROM [SHOW CREATE pkey_comment];
----
COMMENT ON INDEX public.pkey_comment@pkey_comment_pkey IS 'idx';
COMMENT ON INDEX public.pkey_comment@i2 IS 'idx2';
COMMENT ON CONSTRAINT pkey_comment_pkey ON public.pkey_comment IS 'const';
COMMENT ON CONSTRAINT i2 ON public.pkey_comment IS 'idx3'


subtest test-index-deduplication

# Regression for #78046: `ALTER PRIMARY KEY` should not create a secondary
Expand Down
46 changes: 28 additions & 18 deletions pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,30 +1562,40 @@ func (sc *SchemaChanger) done(ctx context.Context) error {
}
}

// If a primary index swap or any indexes are being dropped clean up any
// comments related to it.
// If `m` is a primary index swap, which results in creations of a primary
// index and possibly new, rewritten, secondary indexes, carry over the
// comments associated with the old indexes to the new ones.
if pkSwap := m.AsPrimaryKeySwap(); pkSwap != nil {
id := pkSwap.PrimaryKeySwapDesc().OldPrimaryIndexId
commentsToDelete = append(commentsToDelete,
commentToDelete{
id: int64(scTable.GetID()),
subID: int64(id),
commentType: catalogkeys.IndexCommentType,
})
for i := range pkSwap.PrimaryKeySwapDesc().OldIndexes {
// Skip the primary index.
if pkSwap.PrimaryKeySwapDesc().OldIndexes[i] == id {
continue
pkSwapDesc := pkSwap.PrimaryKeySwapDesc()
oldIndexIDs := append([]descpb.IndexID{pkSwapDesc.OldPrimaryIndexId}, pkSwapDesc.OldIndexes...)
newIndexIDs := append([]descpb.IndexID{pkSwapDesc.NewPrimaryIndexId}, pkSwapDesc.NewIndexes...)
for i := range oldIndexIDs {
oldIndexDesc, err := catalog.MustFindIndexByID(scTable, oldIndexIDs[i])
if err != nil {
return err
}
newIndexDesc, err := catalog.MustFindIndexByID(scTable, newIndexIDs[i])
if err != nil {
return err
}
// Set up a swap operation for any re-created indexes.
commentsToSwap = append(commentsToSwap,
commentToSwap{
id: int64(scTable.GetID()),
oldSubID: int64(pkSwap.PrimaryKeySwapDesc().OldIndexes[i]),
newSubID: int64(pkSwap.PrimaryKeySwapDesc().NewIndexes[i]),
oldSubID: int64(oldIndexDesc.GetID()),
newSubID: int64(newIndexDesc.GetID()),
commentType: catalogkeys.IndexCommentType,
},
)
})
// If index backs a PRIMARY KEY or UNIQUE constraint, carry over the comments associated
// with the constraint as well.
if oldIndexDesc.IsUnique() {
commentsToSwap = append(commentsToSwap,
commentToSwap{
id: int64(scTable.GetID()),
oldSubID: int64(oldIndexDesc.GetConstraintID()),
newSubID: int64(newIndexDesc.GetConstraintID()),
commentType: catalogkeys.ConstraintCommentType,
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/show_create_clauses.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type tableComments struct {
}

type comment struct {
subID int
subID int // the column, or index, or constraint ID that is the subject of this comment.
comment string
}

Expand Down

0 comments on commit 891b356

Please sign in to comment.