Skip to content

Commit 2fc5c62

Browse files
committed
sql: add storage param for region column inference
This commit adds a new storage parameter to the table descriptor `infer_rbr_region_col_using_constraint`. It will be used by following commits/PRs to determine a foreign-key constraint that will be used to look up values for the RBR region column from the FK parent table. Informs #148243 Release note: None
1 parent ab6ffc2 commit 2fc5c62

File tree

26 files changed

+388
-233
lines changed

26 files changed

+388
-233
lines changed

pkg/cli/testdata/doctor/test_recreate_zipdir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ SELECT crdb_internal.unsafe_upsert_namespace_entry(100, 0, 'public', 101, true);
1414
SELECT crdb_internal.unsafe_upsert_descriptor(102, decode('125a0a08706f73746772657310661a300a0b0a0561646d696e100218020a0d0a067075626c696310801018000a0a0a04726f6f74100218021204726f6f741803220028013a0c0a067075626c69631202086740004a005a0210007000', 'hex'), true);
1515
SELECT crdb_internal.unsafe_upsert_namespace_entry(0, 0, 'postgres', 102, true);
1616
SELECT crdb_internal.unsafe_upsert_descriptor(103, decode('2249086612067075626c6963186722310a0b0a0561646d696e100218020a0d0a067075626c696310840418000a0a0a04726f6f7410021802120561646d696e18032a00300140004a007000', 'hex'), true);
17-
SELECT crdb_internal.unsafe_upsert_descriptor(104, decode('0a87030a01741868206428013a0042280a016910011a0e0801104018002a0030005014600020013000680070007800800100880100980100423c0a05726f77696410021a0e0801104018002a0030005014600020002a0e756e697175655f726f77696428293001680070007800800100880100980100480352750a06745f706b6579100118012205726f7769642a0169300240004a10080010001a00200028003000380040005a0070017a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c801d88aed86ddb7c5e517d00101e00100e9010000000000000000f2010060026a210a0b0a0561646d696e100218020a0a0a04726f6f74100218021204726f6f741803800101880103980100b2011b0a077072696d61727910001a01691a05726f776964200120022801b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c00265c80200e00200800300880302a80300b00300d00300d80300e00300f80300880400980400a00400a80400', 'hex'), true);
17+
SELECT crdb_internal.unsafe_upsert_descriptor(104, decode('0a8a030a01741868206428013a0042280a016910011a0e0801104018002a0030005014600020013000680070007800800100880100980100423c0a05726f77696410021a0e0801104018002a0030005014600020002a0e756e697175655f726f77696428293001680070007800800100880100980100480352750a06745f706b6579100118012205726f7769642a0169300240004a10080010001a00200028003000380040005a0070017a0408002000800100880100900104980101a20106080012001800a80100b20100ba0100c00100c801d88aed86ddb7c5e517d00101e00100e9010000000000000000f2010060026a210a0b0a0561646d696e100218020a0a0a04726f6f74100218021204726f6f741803800101880103980100b2011b0a077072696d61727910001a01691a05726f776964200120022801b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c00265c80200e00200800300880302a80300b00300d00300d80300e00300f80300880400980400a00400a80400b00400', 'hex'), true);
1818
COMMIT;

pkg/cli/testdata/doctor/test_recreate_zipdir-json

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

pkg/sql/catalog/bootstrap/testdata/testdata

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

pkg/sql/catalog/catpb/catalog.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ const (
100100
// AutoFullStatsEnabledTableSettingName is the name of the automatic
101101
// full stats collection enabled table setting.
102102
AutoFullStatsEnabledTableSettingName = "sql_stats_automatic_full_collection_enabled"
103+
104+
// RBRUsingConstraintTableSettingName is the name of the REGIONAL BY ROW region
105+
// column inference setting.
106+
RBRUsingConstraintTableSettingName = "infer_rbr_region_col_using_constraint"
103107
)
104108

105109
// AutoStatsCollectionEnabled indicates if automatic statistics collection is

pkg/sql/catalog/descpb/structured.proto

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,12 @@ message TableDescriptor {
13691369
// When forced is set the table's RLS policies are enforced even on the table owner.
13701370
optional bool row_level_security_forced = 69 [(gogoproto.nullable) = false];
13711371

1372-
// Next ID: 70
1372+
// RBRUsingConstraint is the ID of the foreign-key constraint that is used to
1373+
// look up values for the region column of a REGIONAL BY ROW table.
1374+
optional uint32 rbr_using_constraint = 70 [(gogoproto.nullable) = false,
1375+
(gogoproto.customname) = "RBRUsingConstraint", (gogoproto.casttype) = "ConstraintID"];
1376+
1377+
// Next ID: 71
13731378
}
13741379

13751380
// ExternalRowData indicates that the row data for this object is stored outside

pkg/sql/catalog/descriptor.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,11 @@ type TableDescriptor interface {
789789
// GetRegionalByRowTableRegionColumnName returns the region column name of a
790790
// REGIONAL BY ROW table.
791791
GetRegionalByRowTableRegionColumnName() (tree.Name, error)
792+
// GetRegionalByRowUsingConstraint returns the ID of the foreign-key
793+
// constraint that is used to determine the region for each row in a
794+
// REGIONAL BY ROW table. It returns the zero value if the table is not RBR or
795+
// no such constraint is set.
796+
GetRegionalByRowUsingConstraint() descpb.ConstraintID
792797
// GetRowLevelTTL returns the row-level TTL config for the table.
793798
GetRowLevelTTL() *catpb.RowLevelTTL
794799
// HasRowLevelTTL returns where there is a row-level TTL config for the table.
@@ -797,7 +802,7 @@ type TableDescriptor interface {
797802
// to be excluded during backup.
798803
GetExcludeDataFromBackup() bool
799804
// GetStorageParams returns a list of storage parameters for the table.
800-
GetStorageParams(spaceBetweenEqual bool) []string
805+
GetStorageParams(spaceBetweenEqual bool) ([]string, error)
801806
// NoAutoStatsSettingsOverrides is true if no auto stats related settings are
802807
// set at the table level for the given table.
803808
NoAutoStatsSettingsOverrides() bool

pkg/sql/catalog/tabledesc/structured.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,11 @@ func (desc *wrapper) GetRegionalByRowTableRegionColumnName() (tree.Name, error)
24612461
return tree.Name(*colName), nil
24622462
}
24632463

2464+
// GetRegionalByRowUsingConstraint implements the TableDescriptor interface.
2465+
func (desc *wrapper) GetRegionalByRowUsingConstraint() descpb.ConstraintID {
2466+
return desc.RBRUsingConstraint
2467+
}
2468+
24642469
// GetRowLevelTTL implements the TableDescriptor interface.
24652470
func (desc *wrapper) GetRowLevelTTL() *catpb.RowLevelTTL {
24662471
return desc.RowLevelTTL
@@ -2477,7 +2482,7 @@ func (desc *wrapper) GetExcludeDataFromBackup() bool {
24772482
}
24782483

24792484
// GetStorageParams implements the TableDescriptor interface.
2480-
func (desc *wrapper) GetStorageParams(spaceBetweenEqual bool) []string {
2485+
func (desc *wrapper) GetStorageParams(spaceBetweenEqual bool) ([]string, error) {
24812486
var storageParams []string
24822487
var spacing string
24832488
if spaceBetweenEqual {
@@ -2576,7 +2581,17 @@ func (desc *wrapper) GetStorageParams(spaceBetweenEqual bool) []string {
25762581
if desc.IsSchemaLocked() {
25772582
appendStorageParam(`schema_locked`, `true`)
25782583
}
2579-
return storageParams
2584+
if usingFK := desc.GetRegionalByRowUsingConstraint(); usingFK != descpb.ConstraintID(0) {
2585+
// NOTE: when validating the descriptor, we check that the referenced
2586+
// constraint exists, so this should never fail.
2587+
constraint, err := catalog.MustFindConstraintByID(desc, usingFK)
2588+
if err != nil {
2589+
return nil, err
2590+
}
2591+
appendStorageParam(catpb.RBRUsingConstraintTableSettingName,
2592+
fmt.Sprintf("%q", tree.Name(constraint.GetName())))
2593+
}
2594+
return storageParams, nil
25802595
}
25812596

25822597
// GetMultiRegionEnumDependency returns true if the given table has an "implicit"

pkg/sql/catalog/tabledesc/validate_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ var validationMap = []struct {
147147
"NextPolicyID": {status: iSolemnlySwearThisFieldIsValidated},
148148
"RowLevelSecurityEnabled": {status: thisFieldReferencesNoObjects},
149149
"RowLevelSecurityForced": {status: thisFieldReferencesNoObjects},
150+
"RBRUsingConstraint": {status: todoIAmKnowinglyAddingTechDebt,
151+
reason: "TODO(drewk): remove this after adding validation in the next commit"},
150152
},
151153
},
152154
{

0 commit comments

Comments
 (0)