Skip to content

Commit

Permalink
Merge #67712
Browse files Browse the repository at this point in the history
67712: sql: add setting to clone parent partitions for deinterleaving r=otan a=ajwerner

See the release note.

Fixes #66193.

Release note (sql change): Added a new session variable
`enable_copying_partitioning_when_deinterleaving_table` (defaulted to a new
cluster setting
`sql.defaults.copy_partitioning_when_deinterleaving_table.enabled`) which will
change the behavior of ALTER PRIMARY KEY when performing a change which retains
the same primary key but removes an INTERLEAVE INTO clause. When this variable
is true and such an ALTER PRIMARY KEY is run, the partitioning and zone
configuration which applied to the root of the interleave will not be applied
to the new primary index.

Co-authored-by: Andrew Werner <[email protected]>
  • Loading branch information
craig[bot] and ajwerner committed Jul 20, 2021
2 parents 2a91e74 + 95eae51 commit 561b93e
Show file tree
Hide file tree
Showing 10 changed files with 1,025 additions and 377 deletions.
1 change: 1 addition & 0 deletions docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ server.web_session_timeout duration 168h0m0s the duration that a newly created w
sql.cross_db_fks.enabled boolean false if true, creating foreign key references across databases is allowed
sql.cross_db_sequence_owners.enabled boolean false if true, creating sequences owned by tables from other databases is allowed
sql.cross_db_views.enabled boolean false if true, creating views that refer to other databases is allowed
sql.defaults.copy_partitioning_when_deinterleaving_table.enabled boolean false default value for enable_copying_partitioning_when_deinterleaving_table session variable
sql.defaults.default_int_size integer 8 the size, in bytes, of an INT type
sql.defaults.disallow_full_table_scans.enabled boolean false setting to true rejects queries that have planned a full table scan
sql.defaults.distsql enumeration auto default distributed SQL execution mode [off = 0, auto = 1, on = 2]
Expand Down
1 change: 1 addition & 0 deletions docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<tr><td><code>sql.cross_db_fks.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating foreign key references across databases is allowed</td></tr>
<tr><td><code>sql.cross_db_sequence_owners.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating sequences owned by tables from other databases is allowed</td></tr>
<tr><td><code>sql.cross_db_views.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating views that refer to other databases is allowed</td></tr>
<tr><td><code>sql.defaults.copy_partitioning_when_deinterleaving_table.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for enable_copying_partitioning_when_deinterleaving_table session variable</td></tr>
<tr><td><code>sql.defaults.default_int_size</code></td><td>integer</td><td><code>8</code></td><td>the size, in bytes, of an INT type</td></tr>
<tr><td><code>sql.defaults.disallow_full_table_scans.enabled</code></td><td>boolean</td><td><code>false</code></td><td>setting to true rejects queries that have planned a full table scan</td></tr>
<tr><td><code>sql.defaults.distsql</code></td><td>enumeration</td><td><code>auto</code></td><td>default distributed SQL execution mode [off = 0, auto = 1, on = 2]</td></tr>
Expand Down
509 changes: 509 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning_deinterleave

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions pkg/sql/alter_primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"strings"

"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
"github.com/gogo/protobuf/proto"
)

// alterPrimaryKeyLocalitySwap contains metadata on a locality swap for
Expand Down Expand Up @@ -356,6 +358,12 @@ func (p *planner) AlterPrimaryKey(
if err != nil {
return err
}
} else {
if err := maybeCopyPartitioningWhenDeinterleaving(
ctx, p, tableDesc, newPrimaryIndexDesc,
); err != nil {
return err
}
}

if partitionAllBy != nil {
Expand Down Expand Up @@ -550,6 +558,94 @@ func (p *planner) AlterPrimaryKey(
return nil
}

func maybeCopyPartitioningWhenDeinterleaving(
ctx context.Context,
p *planner,
tableDesc *tabledesc.Mutable,
newPrimaryIndexDesc *descpb.IndexDescriptor,
) error {
if tableDesc.GetPrimaryIndex().NumInterleaveAncestors() == 0 ||
!p.SessionData().CopyPartitioningWhenDeinterleavingTable ||
len(newPrimaryIndexDesc.Interleave.Ancestors) > 0 {
return nil
}

// The old primary key was interleaved in a parent and the new one is not.
// In this case, we need to clone out the old primary key's partitioning
// and zone configs and apply them to the new primary index. We do this
// if the old primary index and the new primary index have the exact same
// columns. That also allows us to side-step discussions of what to do
// about partitioning for any newly created unique index we might create
// below.

root := tableDesc.GetPrimaryIndex().GetInterleaveAncestor(0)
interleaveRoot, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, root.TableID, tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{
Required: true,
AvoidCached: true,
},
DesiredObjectKind: tree.TableObject,
})
if err != nil {
return errors.Wrap(err, "looking up interleaved root")
}
rootIndex, err := interleaveRoot.FindIndexWithID(root.IndexID)
if err != nil {
return errors.Wrap(err, "looking up interleaved root index")
}

// If the new primary key does not have the interleave root as a prefix,
// do not copy the interleave.
if rootKeys := rootIndex.IndexDesc().KeyColumnIDs; !descpb.ColumnIDs.Equals(
rootKeys, newPrimaryIndexDesc.KeyColumnIDs[:len(rootKeys)],
) {
return nil
}

// The parent is not partitioned, return.
if rootIndex.GetPartitioning().NumColumns() == 0 {
return nil
}
newPrimaryIndexDesc.Partitioning = *rootIndex.GetPartitioning().DeepCopy().PartitioningDesc()
rootCfg, err := getZoneConfigRaw(ctx, p.txn, p.execCfg.Codec, root.TableID)
if err != nil {
return errors.Wrapf(err, "retrieving zone config for table %s [%d]",
interleaveRoot.GetName(), interleaveRoot.GetID())
}
tableCfg, err := getZoneConfigRaw(ctx, p.txn, p.execCfg.Codec, tableDesc.GetID())
if err != nil {
return errors.Wrapf(err, "retrieving zone config for table %s [%d]",
tableDesc.GetName(), tableDesc.GetID())
}
// Initialize the zone config for the child. We expect it to be nil because
// the table was an interleaved child and we did not allow such children to
// have zone configs. It may be a subzone placeholder because other indexes
// might be partitioned and have zone configs.
if tableCfg == nil {
// Marking NumReplicas as 0 indicates that this zone config is a
// subzone placeholder. We assume that the value in copying out the
// partitioning is to copy out the configuration as it applies to the
// partitions of the primary index.
tableCfg = &zonepb.ZoneConfig{
NumReplicas: proto.Int(0),
}
} else if !tableCfg.IsSubzonePlaceholder() {
return errors.AssertionFailedf("child table %s [%d] of interleave was not a subzone placeholder",
tableDesc.GetName(), tableDesc.GetID())
}

for _, s := range rootCfg.Subzones {
if s.IndexID == uint32(root.IndexID) {
s.IndexID = uint32(newPrimaryIndexDesc.ID)
tableCfg.Subzones = append(tableCfg.Subzones, s)
}
}
_, err = writeZoneConfig(
ctx, p.txn, tableDesc.GetID(), tableDesc, tableCfg, p.execCfg, true,
)
return err
}

// Given the current table descriptor and the new primary keys
// index descriptor this function determines if the two are
// equivalent and if any index creation operations are needed
Expand Down
14 changes: 13 additions & 1 deletion pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ var experimentalComputedColumnRewrites = settings.RegisterValidatedStringSetting
},
)

var copyPartitioningWhenDeinterleavingTable = settings.RegisterBoolSetting(
`sql.defaults.copy_partitioning_when_deinterleaving_table.enabled`,
`default value for enable_copying_partitioning_when_deinterleaving_table session variable`,
false,
).WithPublic()

// settingWorkMemBytes is a cluster setting that determines the maximum amount
// of RAM that a processor can use.
var settingWorkMemBytes = settings.RegisterByteSizeSetting(
Expand Down Expand Up @@ -2541,11 +2547,17 @@ func (m *sessionDataMutator) SetIntervalStyle(style duration.IntervalStyle) {
m.data.DataConversionConfig.IntervalStyle = style
}

// SetStubCatalogTableEnabled sets default value for stub_catalog_tables.
// SetStubCatalogTablesEnabled sets default value for stub_catalog_tables.
func (m *sessionDataMutator) SetStubCatalogTablesEnabled(enabled bool) {
m.data.StubCatalogTablesEnabled = enabled
}

// SetCopyPartitioningWhenDeinterleavingTable sets the value for
// CopyPartitioningWhenDeinterleavingTable.
func (m *sessionDataMutator) SetCopyPartitioningWhenDeinterleavingTable(b bool) {
m.data.CopyPartitioningWhenDeinterleavingTable = b
}

func (m *sessionDataMutator) SetExperimentalComputedColumnRewrites(val string) {
m.data.ExperimentalComputedColumnRewrites = val
}
Expand Down
155 changes: 78 additions & 77 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -4231,83 +4231,84 @@ subtest variables
query TT colnames
SELECT * FROM information_schema.session_variables where variable not in ('crdb_version', 'session_id', 'distsql', 'vectorize', 'experimental_distsql_planning')
----
variable value
allow_prepare_as_opt_plan off
application_name ·
backslash_quote safe_encoding
bytea_output hex
client_encoding UTF8
client_min_messages notice
database test
datestyle ISO, MDY
default_int_size 8
default_tablespace ·
default_transaction_isolation serializable
default_transaction_priority normal
default_transaction_read_only off
default_transaction_use_follower_reads off
disable_partially_distributed_plans off
disallow_full_table_scans off
distsql_workmem 64 MiB
enable_drop_enum_value off
enable_experimental_alter_column_type_general off
enable_experimental_stream_replication off
enable_implicit_select_for_update on
enable_insert_fast_path on
enable_seqscan on
enable_zigzag_join on
escape_string_warning on
experimental_computed_column_rewrites ·
experimental_enable_expression_indexes off
experimental_enable_hash_sharded_indexes off
experimental_enable_implicit_column_partitioning off
experimental_enable_temp_tables off
experimental_enable_unique_without_index_constraints on
experimental_use_new_schema_changer off
extra_float_digits 0
force_savepoint_restart off
foreign_key_cascades_limit 10000
idle_in_session_timeout 0
idle_in_transaction_session_timeout 0
integer_datetimes on
intervalstyle postgres
locality region=test,dc=dc1
locality_optimized_partitioned_index_scan on
lock_timeout 0
max_identifier_length 128
max_index_keys 32
node_id 1
optimizer on
optimizer_use_histograms on
optimizer_use_multicol_stats on
override_multi_region_zone_config off
prefer_lookup_joins_for_fks off
reorder_joins_limit 8
require_explicit_primary_keys off
results_buffer_size 16384
row_security off
save_tables_prefix ·
search_path $user,public
serial_normalization rowid
server_encoding UTF8
server_version 13.0.0
server_version_num 130000
session_authorization root
session_user root
sql_safe_updates off
ssl_renegotiation_limit 0
standard_conforming_strings on
statement_timeout 0
stub_catalog_tables on
synchronize_seqscans on
synchronous_commit on
testing_vectorize_inject_panics off
timezone UTC
tracing off
transaction_isolation serializable
transaction_priority normal
transaction_read_only off
transaction_status NoTxn
variable value
allow_prepare_as_opt_plan off
application_name ·
backslash_quote safe_encoding
bytea_output hex
client_encoding UTF8
client_min_messages notice
database test
datestyle ISO, MDY
default_int_size 8
default_tablespace ·
default_transaction_isolation serializable
default_transaction_priority normal
default_transaction_read_only off
default_transaction_use_follower_reads off
disable_partially_distributed_plans off
disallow_full_table_scans off
distsql_workmem 64 MiB
enable_copying_partitioning_when_deinterleaving_table off
enable_drop_enum_value off
enable_experimental_alter_column_type_general off
enable_experimental_stream_replication off
enable_implicit_select_for_update on
enable_insert_fast_path on
enable_seqscan on
enable_zigzag_join on
escape_string_warning on
experimental_computed_column_rewrites ·
experimental_enable_expression_indexes off
experimental_enable_hash_sharded_indexes off
experimental_enable_implicit_column_partitioning off
experimental_enable_temp_tables off
experimental_enable_unique_without_index_constraints on
experimental_use_new_schema_changer off
extra_float_digits 0
force_savepoint_restart off
foreign_key_cascades_limit 10000
idle_in_session_timeout 0
idle_in_transaction_session_timeout 0
integer_datetimes on
intervalstyle postgres
locality region=test,dc=dc1
locality_optimized_partitioned_index_scan on
lock_timeout 0
max_identifier_length 128
max_index_keys 32
node_id 1
optimizer on
optimizer_use_histograms on
optimizer_use_multicol_stats on
override_multi_region_zone_config off
prefer_lookup_joins_for_fks off
reorder_joins_limit 8
require_explicit_primary_keys off
results_buffer_size 16384
row_security off
save_tables_prefix ·
search_path $user,public
serial_normalization rowid
server_encoding UTF8
server_version 13.0.0
server_version_num 130000
session_authorization root
session_user root
sql_safe_updates off
ssl_renegotiation_limit 0
standard_conforming_strings on
statement_timeout 0
stub_catalog_tables on
synchronize_seqscans on
synchronous_commit on
testing_vectorize_inject_panics off
timezone UTC
tracing off
transaction_isolation serializable
transaction_priority normal
transaction_read_only off
transaction_status NoTxn

# information_schema can be used with the anonymous database.
# It should show information across all databases.
Expand Down
Loading

0 comments on commit 561b93e

Please sign in to comment.