Skip to content

Commit 0a62e01

Browse files
committed
sql/schemachanger: fix RecreateTargetIndexID when deflating indexes
Previously, the logic to rewrite RecreateTargetIndexID could pick the incorrect index when reducing to fewer indexes. To address, this patch selects the next suitable index to publish the secondary index with. Release note: None
1 parent 881d2d7 commit 0a62e01

File tree

36 files changed

+903
-937
lines changed

36 files changed

+903
-937
lines changed

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,12 @@ func addASwapInIndexByCloningFromSource(
14671467
//
14681468
// Note that this function excludes acting upon indexes whose IDs are in `excludes`.
14691469
func updateElementsToDependOnNewFromOld(
1470-
b BuildCtx, tableID catid.DescID, old catid.IndexID, new catid.IndexID, excludes catid.IndexSet,
1470+
b BuildCtx,
1471+
tableID catid.DescID,
1472+
old catid.IndexID,
1473+
new catid.IndexID,
1474+
newRecreateTargetID catid.IndexID,
1475+
excludes catid.IndexSet,
14711476
) {
14721477
b.QueryByID(tableID).ForEach(func(current scpb.Status, target scpb.TargetStatus, e scpb.Element) {
14731478
switch e := e.(type) {
@@ -1483,8 +1488,8 @@ func updateElementsToDependOnNewFromOld(
14831488
if e.SourceIndexID == old && !excludes.Contains(e.IndexID) {
14841489
e.SourceIndexID = new
14851490
}
1486-
if e.RecreateTargetIndexID == old {
1487-
e.RecreateTargetIndexID = new
1491+
if e.RecreateTargetIndexID == old && !excludes.Contains(e.IndexID) {
1492+
e.RecreateTargetIndexID = newRecreateTargetID
14881493
}
14891494
case *scpb.CheckConstraint:
14901495
if e.IndexIDForValidation == old {
@@ -1553,7 +1558,7 @@ func (pic *primaryIndexChain) inflate(b BuildCtx) {
15531558
b BuildCtx, tableID catid.DescID, out catid.IndexID, source catid.IndexID, isInFinal bool,
15541559
) (in, inTemp indexSpec) {
15551560
in, inTemp = addASwapInIndexByCloningFromSource(b, tableID, out, source, isInFinal)
1556-
updateElementsToDependOnNewFromOld(b, tableID, out, in.primary.IndexID,
1561+
updateElementsToDependOnNewFromOld(b, tableID, out, in.primary.IndexID, in.primary.IndexID,
15571562
catid.MakeIndexIDSet(in.primary.IndexID, in.primary.TemporaryIndexID))
15581563
return in, inTemp
15591564
}
@@ -1638,6 +1643,14 @@ func (pic *primaryIndexChain) deflate(b BuildCtx) {
16381643
redundants = append(redundants, idxSpec)
16391644
redundantIDs[idxSpec] = true
16401645
}
1646+
hasRedundantID := func(id catid.IndexID) bool {
1647+
for _, r := range redundants {
1648+
if r.indexID() == id {
1649+
return true
1650+
}
1651+
}
1652+
return false
1653+
}
16411654

16421655
if haveSameIndexCols(b, tableID, pic.oldSpec.primary.IndexID, pic.inter1Spec.primary.IndexID) {
16431656
markAsRedundant(&pic.inter1Spec)
@@ -1678,8 +1691,28 @@ func (pic *primaryIndexChain) deflate(b BuildCtx) {
16781691
// identified by attrs defined in screl and updating SourceIndexID of a
16791692
// primary index will cause us to fail to retrieve the element to drop).
16801693
for _, redundant := range redundants {
1694+
// For new replacement secondary indexes we need to select the index to
1695+
// publish this index with. The source index ID is an excellent candidate
1696+
// as long as its not the old primary index (i.e. we folded all earlier
1697+
// primary indexes).
1698+
recreateTargetID := redundant.SourceIndexID()
1699+
if recreateTargetID == pic.oldSpec.primary.IndexID || hasRedundantID(recreateTargetID) {
1700+
// Otherwise, we need to select the next valid index in the chain, that
1701+
// follows the redundant one.
1702+
firstMatch := false
1703+
specs := pic.allPrimaryIndexSpecs(func(spec *indexSpec) bool {
1704+
if spec.indexID() == recreateTargetID {
1705+
firstMatch = true
1706+
return false
1707+
}
1708+
return !redundantIDs[spec] && firstMatch
1709+
})
1710+
if len(specs) > 0 {
1711+
recreateTargetID = specs[0].indexID()
1712+
}
1713+
}
16811714
updateElementsToDependOnNewFromOld(b, tableID,
1682-
redundant.indexID(), redundant.SourceIndexID(), catid.IndexSet{} /* excludes */)
1715+
redundant.indexID(), redundant.SourceIndexID(), recreateTargetID, catid.IndexSet{} /* excludes */)
16831716
*redundant = indexSpec{} // reset this indexSpec in the chain
16841717
}
16851718

pkg/sql/schemachanger/testdata/end_to_end/alter_table_alter_primary_key_using_hash/alter_table_alter_primary_key_using_hash.explain

Lines changed: 18 additions & 19 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)