Skip to content

Commit

Permalink
Merge #50591
Browse files Browse the repository at this point in the history
50591: sql: stop populating legacy descriptor fields for FKs r=RaduBerinde a=RaduBerinde

The `LegacyOriginIndex` and `LegacyReferencedIndex` fields of
`ForeignKeyConstraint` proto are written for backward compatibility with 19.2.
We no longer need them and there will be no value to set for the origin index
once we won't require one.

Release note: None

Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
  • Loading branch information
craig[bot] and RaduBerinde committed Jun 24, 2020
2 parents bf353a0 + d98a3cd commit 8523caa
Show file tree
Hide file tree
Showing 4 changed files with 350 additions and 448 deletions.
35 changes: 14 additions & 21 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,15 +760,12 @@ func ResolveFK(
for i, col := range originCols {
originColumnIDs[i] = col.ID
}
var legacyOriginIndexID sqlbase.IndexID
// Search for an index on the origin table that matches. If one doesn't exist,
// we create one automatically if the table to alter is new or empty. We also
// search if an index for the set of columns was created in this transaction.
originIdx, err := sqlbase.FindFKOriginIndexInTxn(tbl, originColumnIDs)
if err == nil {
// If there was no error, we found a suitable index.
legacyOriginIndexID = originIdx.ID
} else {
_, err = sqlbase.FindFKOriginIndexInTxn(tbl, originColumnIDs)
// If there was no error, we found a suitable index.
if err != nil {
// No existing suitable index was found.
if ts == NonEmptyTable {
var colNames bytes.Buffer
Expand All @@ -787,18 +784,16 @@ func ResolveFK(
return pgerror.Newf(pgcode.ForeignKeyViolation,
"foreign key requires an existing index on columns %s", colNames.String())
}
id, err := addIndexForFK(tbl, originCols, constraintName, ts)
_, err := addIndexForFK(tbl, originCols, constraintName, ts)
if err != nil {
return err
}
legacyOriginIndexID = id
}

referencedIdx, err := sqlbase.FindFKReferencedIndex(target.TableDesc(), targetColIDs)
_, err = sqlbase.FindFKReferencedIndex(target.TableDesc(), targetColIDs)
if err != nil {
return err
}
legacyReferencedIndexID := referencedIdx.ID

var validity sqlbase.ConstraintValidity
if ts != NewTable {
Expand All @@ -810,17 +805,15 @@ func ResolveFK(
}

ref := sqlbase.ForeignKeyConstraint{
OriginTableID: tbl.ID,
OriginColumnIDs: originColumnIDs,
ReferencedColumnIDs: targetColIDs,
ReferencedTableID: target.ID,
Name: constraintName,
Validity: validity,
OnDelete: sqlbase.ForeignKeyReferenceActionValue[d.Actions.Delete],
OnUpdate: sqlbase.ForeignKeyReferenceActionValue[d.Actions.Update],
Match: sqlbase.CompositeKeyMatchMethodValue[d.Match],
LegacyOriginIndex: legacyOriginIndexID,
LegacyReferencedIndex: legacyReferencedIndexID,
OriginTableID: tbl.ID,
OriginColumnIDs: originColumnIDs,
ReferencedColumnIDs: targetColIDs,
ReferencedTableID: target.ID,
Name: constraintName,
Validity: validity,
OnDelete: sqlbase.ForeignKeyReferenceActionValue[d.Actions.Delete],
OnUpdate: sqlbase.ForeignKeyReferenceActionValue[d.Actions.Update],
Match: sqlbase.CompositeKeyMatchMethodValue[d.Match],
}

if ts == NewTable {
Expand Down
60 changes: 27 additions & 33 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,17 +1029,15 @@ func maybeUpgradeForeignKeyRepOnIndex(
}
numCols := ref.SharedPrefixLen
outFK := ForeignKeyConstraint{
OriginTableID: desc.ID,
OriginColumnIDs: idx.ColumnIDs[:numCols],
ReferencedTableID: ref.Table,
ReferencedColumnIDs: referencedIndex.ColumnIDs[:numCols],
Name: ref.Name,
Validity: ref.Validity,
OnDelete: ref.OnDelete,
OnUpdate: ref.OnUpdate,
Match: ref.Match,
LegacyOriginIndex: idx.ID,
LegacyReferencedIndex: referencedIndex.ID,
OriginTableID: desc.ID,
OriginColumnIDs: idx.ColumnIDs[:numCols],
ReferencedTableID: ref.Table,
ReferencedColumnIDs: referencedIndex.ColumnIDs[:numCols],
Name: ref.Name,
Validity: ref.Validity,
OnDelete: ref.OnDelete,
OnUpdate: ref.OnUpdate,
Match: ref.Match,
}
desc.OutboundFKs = append(desc.OutboundFKs, outFK)
}
Expand Down Expand Up @@ -1097,34 +1095,30 @@ func maybeUpgradeForeignKeyRepOnIndex(
otherTable.ID, ref)
}
inFK = ForeignKeyConstraint{
OriginTableID: ref.Table,
OriginColumnIDs: forwardFK.OriginColumnIDs,
ReferencedTableID: desc.ID,
ReferencedColumnIDs: forwardFK.ReferencedColumnIDs,
Name: forwardFK.Name,
Validity: forwardFK.Validity,
OnDelete: forwardFK.OnDelete,
OnUpdate: forwardFK.OnUpdate,
Match: forwardFK.Match,
LegacyOriginIndex: originIndex.ID,
LegacyReferencedIndex: idx.ID,
OriginTableID: ref.Table,
OriginColumnIDs: forwardFK.OriginColumnIDs,
ReferencedTableID: desc.ID,
ReferencedColumnIDs: forwardFK.ReferencedColumnIDs,
Name: forwardFK.Name,
Validity: forwardFK.Validity,
OnDelete: forwardFK.OnDelete,
OnUpdate: forwardFK.OnUpdate,
Match: forwardFK.Match,
}
} else {
// We have an old (not upgraded yet) table, with a matching forward
// foreign key.
numCols := originIndex.ForeignKey.SharedPrefixLen
inFK = ForeignKeyConstraint{
OriginTableID: ref.Table,
OriginColumnIDs: originIndex.ColumnIDs[:numCols],
ReferencedTableID: desc.ID,
ReferencedColumnIDs: idx.ColumnIDs[:numCols],
Name: originIndex.ForeignKey.Name,
Validity: originIndex.ForeignKey.Validity,
OnDelete: originIndex.ForeignKey.OnDelete,
OnUpdate: originIndex.ForeignKey.OnUpdate,
Match: originIndex.ForeignKey.Match,
LegacyOriginIndex: originIndex.ID,
LegacyReferencedIndex: idx.ID,
OriginTableID: ref.Table,
OriginColumnIDs: originIndex.ColumnIDs[:numCols],
ReferencedTableID: desc.ID,
ReferencedColumnIDs: idx.ColumnIDs[:numCols],
Name: originIndex.ForeignKey.Name,
Validity: originIndex.ForeignKey.Validity,
OnDelete: originIndex.ForeignKey.OnDelete,
OnUpdate: originIndex.ForeignKey.OnUpdate,
Match: originIndex.ForeignKey.Match,
}
}
desc.InboundFKs = append(desc.InboundFKs, inFK)
Expand Down
Loading

0 comments on commit 8523caa

Please sign in to comment.