Skip to content

Commit c14033f

Browse files
committed
sql: change ordering of (re)creating secondary indexes in ALTER PK
If we alter primary key on a table with secondary indexes, we will recreate those secondary indexes as well as creating a new unique secondary index on the old primary key column(s). Previously, in the legacy schema changer, we'd create the new unique secondary index first and then recreate all existing secondary index. This is different from how declarative schema changer process ALTER PK, in which the ordering is the opposite. This is not an issue per se; the only time it shows a difference is when you inspect the table after altering PK via, say, `SHOW CREATE t`, where you'd then observe a slightly different ordering of the indexes. This is actually easier to change the legacy schema changer so both schema changer would produce the same ordering of secondary indexes after a ALTER PK. This will help clean several test cases. Release note: None
1 parent f75f543 commit c14033f

File tree

7 files changed

+54
-123
lines changed

7 files changed

+54
-123
lines changed

pkg/ccl/logictestccl/testdata/logic_test/partitioning_implicit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ CREATE TABLE public.t (
599599
u STRING NULL,
600600
e INT8 NOT NULL,
601601
CONSTRAINT t_pkey PRIMARY KEY (pk2 ASC),
602-
UNIQUE INDEX t_pk_key (pk ASC),
603602
UNIQUE INDEX t_u_key (u ASC),
604603
INDEX t_a_idx (a ASC),
605604
UNIQUE INDEX t_b_key (b ASC),
@@ -608,13 +607,15 @@ CREATE TABLE public.t (
608607
INDEX created_idx (c ASC),
609608
UNIQUE INDEX t_e_key (e ASC),
610609
UNIQUE INDEX unique_c_d (c ASC, d ASC),
610+
UNIQUE INDEX t_pk_key (pk ASC),
611611
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u, e)
612612
) PARTITION ALL BY LIST (partition_by) (
613613
PARTITION one VALUES IN ((1)),
614614
PARTITION two VALUES IN ((2))
615615
)
616616
-- Warning: Partitioned table with no zone configurations.
617617

618+
618619
query TTB colnames
619620
SELECT index_name, column_name, implicit FROM crdb_internal.index_columns
620621
WHERE descriptor_name = 't' AND column_type = 'key'

pkg/ccl/logictestccl/testdata/logic_test/regional_by_row

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,12 @@ CREATE TABLE public.regional_by_row_table (
531531
j JSONB NULL,
532532
crdb_region multi_region_test_db.public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::multi_region_test_db.public.crdb_internal_region,
533533
CONSTRAINT regional_by_row_table_pkey PRIMARY KEY (pk2 ASC),
534-
UNIQUE INDEX regional_by_row_table_pk_key (pk ASC),
535534
INDEX regional_by_row_table_a_idx (a ASC),
536535
UNIQUE INDEX regional_by_row_table_b_key (b ASC),
537536
INVERTED INDEX regional_by_row_table_j_idx (j),
538537
UNIQUE INDEX uniq_idx (a ASC) WHERE b > 0:::INT8,
539538
UNIQUE INDEX unique_b_a (b ASC, a ASC),
539+
UNIQUE INDEX regional_by_row_table_pk_key (pk ASC),
540540
FAMILY fam_0_pk_pk2_a_b_j_crdb_region (pk, pk2, a, b, j, crdb_region)
541541
) LOCALITY REGIONAL BY ROW;
542542
ALTER PARTITION "us-east-1" OF INDEX multi_region_test_db.public.regional_by_row_table@regional_by_row_table_a_idx CONFIGURE ZONE USING "gc.ttlseconds" = 10

pkg/ccl/logictestccl/testdata/logic_test/zone

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,6 @@ t CREATE TABLE public.t (
11311131
z INT8 NULL,
11321132
w INT8 NULL,
11331133
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
1134-
UNIQUE INDEX t_x_key (x ASC),
11351134
INDEX i1 (z ASC) PARTITION BY LIST (z) (
11361135
PARTITION p1 VALUES IN ((1), (2)),
11371136
PARTITION p2 VALUES IN ((3), (4))
@@ -1140,6 +1139,7 @@ t CREATE TABLE public.t (
11401139
PARTITION p3 VALUES IN ((5), (6)),
11411140
PARTITION p4 VALUES IN ((7), (8))
11421141
),
1142+
UNIQUE INDEX t_x_key (x ASC),
11431143
FAMILY fam_0_x_y_z_w (x, y, z, w)
11441144
);
11451145
ALTER PARTITION p1 OF INDEX test.public.t@i1 CONFIGURE ZONE USING

pkg/ccl/partitionccl/zone_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,12 +1026,12 @@ func TestPrimaryKeyChangeZoneConfigs(t *testing.T) {
10261026

10271027
// Our subzones should be spans prefixed with dropped copy of i1,
10281028
// dropped copy of i2, new copy of i1, and new copy of i2.
1029-
// These have ID's 2, 3, 8 and 10 respectively.
1029+
// These have ID's 2, 3, 6 and 8 respectively.
10301030
expectedSpans := []roachpb.Key{
10311031
table.IndexSpan(codec, 2 /* indexID */).Key,
10321032
table.IndexSpan(codec, 3 /* indexID */).Key,
1033+
table.IndexSpan(codec, 6 /* indexID */).Key,
10331034
table.IndexSpan(codec, 8 /* indexID */).Key,
1034-
table.IndexSpan(codec, 10 /* indexID */).Key,
10351035
}
10361036
if len(zone.SubzoneSpans) != len(expectedSpans) {
10371037
t.Fatalf("expected subzones to have length %d", len(expectedSpans))

pkg/sql/alter_primary_key.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -377,40 +377,6 @@ func (p *planner) AlterPrimaryKey(
377377
)
378378
}
379379

380-
// Create a new index that indexes everything the old primary index
381-
// does, but doesn't store anything.
382-
if shouldCopyPrimaryKey(tableDesc, newPrimaryIndexDesc, alterPrimaryKeyLocalitySwap) {
383-
newUniqueIdx := tableDesc.GetPrimaryIndex().IndexDescDeepCopy()
384-
// Clear the following fields so that they get generated by AllocateIDs.
385-
newUniqueIdx.ID = 0
386-
newUniqueIdx.Name = ""
387-
newUniqueIdx.StoreColumnIDs = nil
388-
newUniqueIdx.StoreColumnNames = nil
389-
newUniqueIdx.KeySuffixColumnIDs = nil
390-
newUniqueIdx.CompositeColumnIDs = nil
391-
newUniqueIdx.KeyColumnIDs = nil
392-
// Set correct version and encoding type.
393-
//
394-
// TODO(postamar): bump version to LatestIndexDescriptorVersion in 22.2
395-
// This is not possible until then because of a limitation in 21.2 which
396-
// affects mixed-21.2-22.1-version clusters (issue #78426).
397-
newUniqueIdx.Version = descpb.StrictIndexColumnIDGuaranteesVersion
398-
newUniqueIdx.EncodingType = catenumpb.SecondaryIndexEncoding
399-
if err := addIndexMutationWithSpecificPrimaryKey(ctx, tableDesc, &newUniqueIdx, newPrimaryIndexDesc, p.ExecCfg().Settings); err != nil {
400-
return err
401-
}
402-
// Copy the old zone configuration into the newly created unique index for PARTITION ALL BY.
403-
if tableDesc.IsLocalityRegionalByRow() {
404-
if err := p.configureZoneConfigForNewIndexPartitioning(
405-
ctx,
406-
tableDesc,
407-
newUniqueIdx,
408-
); err != nil {
409-
return err
410-
}
411-
}
412-
}
413-
414380
// We have to rewrite all indexes that either:
415381
// * depend on uniqueness from the old primary key (inverted, non-unique, or unique with nulls).
416382
// * don't store or index all columns in the new primary key.
@@ -539,6 +505,40 @@ func (p *planner) AlterPrimaryKey(
539505
newIndexIDs = append(newIndexIDs, newIndex.ID)
540506
}
541507

508+
// Create a new index that indexes everything the old primary index
509+
// does, but doesn't store anything.
510+
if shouldCopyPrimaryKey(tableDesc, newPrimaryIndexDesc, alterPrimaryKeyLocalitySwap) {
511+
newUniqueIdx := tableDesc.GetPrimaryIndex().IndexDescDeepCopy()
512+
// Clear the following fields so that they get generated by AllocateIDs.
513+
newUniqueIdx.ID = 0
514+
newUniqueIdx.Name = ""
515+
newUniqueIdx.StoreColumnIDs = nil
516+
newUniqueIdx.StoreColumnNames = nil
517+
newUniqueIdx.KeySuffixColumnIDs = nil
518+
newUniqueIdx.CompositeColumnIDs = nil
519+
newUniqueIdx.KeyColumnIDs = nil
520+
// Set correct version and encoding type.
521+
//
522+
// TODO(postamar): bump version to LatestIndexDescriptorVersion in 22.2
523+
// This is not possible until then because of a limitation in 21.2 which
524+
// affects mixed-21.2-22.1-version clusters (issue #78426).
525+
newUniqueIdx.Version = descpb.StrictIndexColumnIDGuaranteesVersion
526+
newUniqueIdx.EncodingType = catenumpb.SecondaryIndexEncoding
527+
if err := addIndexMutationWithSpecificPrimaryKey(ctx, tableDesc, &newUniqueIdx, newPrimaryIndexDesc, p.ExecCfg().Settings); err != nil {
528+
return err
529+
}
530+
// Copy the old zone configuration into the newly created unique index for PARTITION ALL BY.
531+
if tableDesc.IsLocalityRegionalByRow() {
532+
if err := p.configureZoneConfigForNewIndexPartitioning(
533+
ctx,
534+
tableDesc,
535+
newUniqueIdx,
536+
); err != nil {
537+
return err
538+
}
539+
}
540+
}
541+
542542
// Determine if removing this index would lead to the uniqueness for a foreign
543543
// key back reference, which will cause this swap operation to be blocked.
544544
const withSearchForReplacement = true

pkg/sql/logictest/testdata/logic_test/alter_primary_key

Lines changed: 12 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ t CREATE TABLE public.t (
269269
crdb_internal_z_shard_4 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(z)), 4:::INT8)) VIRTUAL,
270270
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
271271
UNIQUE INDEX i3 (z ASC) STORING (y),
272-
UNIQUE INDEX t_x_key (x ASC),
273272
INDEX i1 (w ASC),
274273
INDEX i2 (y ASC),
275274
UNIQUE INDEX i4 (z ASC),
276275
UNIQUE INDEX i5 (w ASC) STORING (y),
277276
INVERTED INDEX i6 (v),
278277
INDEX i7 (z ASC) USING HASH WITH (bucket_count=4),
278+
UNIQUE INDEX t_x_key (x ASC),
279279
FAMILY fam_0_x_y_z_w_v (x, y, z, w, v)
280280
)
281281

@@ -287,13 +287,13 @@ SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_na
287287
----
288288
4 i3
289289
9 t_pkey
290-
11 t_x_key
291-
13 i1
292-
15 i2
293-
17 i4
294-
19 i5
295-
21 i6
296-
23 i7
290+
11 i1
291+
13 i2
292+
15 i4
293+
17 i5
294+
19 i6
295+
21 i7
296+
23 t_x_key
297297

298298
# Make sure that each index can index join against the new primary key;
299299

@@ -429,8 +429,8 @@ t CREATE TABLE public.t (
429429
crdb_internal_z_shard_5 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(z)), 5:::INT8)) VIRTUAL,
430430
crdb_internal_y_shard_10 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(y)), 10:::INT8)) VIRTUAL,
431431
CONSTRAINT t_pkey PRIMARY KEY (y ASC) USING HASH WITH (bucket_count=10),
432-
UNIQUE INDEX t_x_key (x ASC),
433432
INDEX i1 (z ASC) USING HASH WITH (bucket_count=5),
433+
UNIQUE INDEX t_x_key (x ASC),
434434
FAMILY fam_0_x_y_z (x, y, z)
435435
)
436436

@@ -448,8 +448,8 @@ query IT
448448
SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_name = 't' ORDER BY index_id
449449
----
450450
3 t_pkey
451-
5 t_x_key
452-
7 i1
451+
5 i1
452+
7 t_x_key
453453

454454
query III
455455
SELECT * FROM t@t_pkey
@@ -494,8 +494,8 @@ t CREATE TABLE public.t (
494494
y INT8 NOT NULL,
495495
z INT8 NULL,
496496
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
497-
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH (bucket_count=5),
498497
INDEX i (z ASC),
498+
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH (bucket_count=5),
499499
FAMILY fam_0_x_y_z (x, y, z)
500500
)
501501

@@ -1397,23 +1397,6 @@ a b k
13971397
statement ok
13981398
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (k);
13991399

1400-
# Legacy schema changer and declarative schema changer will produce slightly
1401-
# different secondary index ordering for alter primary key, hence we assert
1402-
# separately with slightly different expected output.
1403-
onlyif config local-legacy-schema-changer
1404-
query T
1405-
SELECT create_statement FROM [SHOW CREATE TABLE t];
1406-
----
1407-
CREATE TABLE public.t (
1408-
a INT8 NOT NULL,
1409-
b INT8 NOT NULL,
1410-
k INT8 NOT NULL AS (a + b) VIRTUAL,
1411-
CONSTRAINT t_pkey PRIMARY KEY (k ASC),
1412-
UNIQUE INDEX t_a_key (a ASC),
1413-
INDEX t_idx_b_k (b ASC, k ASC)
1414-
)
1415-
1416-
skipif config local-legacy-schema-changer
14171400
query T
14181401
SELECT create_statement FROM [SHOW CREATE TABLE t];
14191402
----
@@ -1450,24 +1433,6 @@ a b k
14501433
statement ok
14511434
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (b, k);
14521435

1453-
# Legacy schema changer and declarative schema changer will produce slightly
1454-
# different secondary index ordering for alter primary key, hence we assert
1455-
# separately with slightly different expected output.
1456-
onlyif config local-legacy-schema-changer
1457-
query T
1458-
SELECT create_statement FROM [SHOW CREATE TABLE t];
1459-
----
1460-
CREATE TABLE public.t (
1461-
a INT8 NOT NULL,
1462-
b INT8 NOT NULL,
1463-
k INT8 NOT NULL AS (a + b) VIRTUAL,
1464-
CONSTRAINT t_pkey PRIMARY KEY (b ASC, k ASC),
1465-
UNIQUE INDEX t_k_key (k ASC),
1466-
UNIQUE INDEX t_a_key (a ASC),
1467-
INDEX t_idx_b_k (b ASC, k ASC)
1468-
)
1469-
1470-
skipif config local-legacy-schema-changer
14711436
query T
14721437
SELECT create_statement FROM [SHOW CREATE TABLE t];
14731438
----
@@ -1512,25 +1477,6 @@ a b k
15121477
statement ok
15131478
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (a);
15141479

1515-
# Legacy schema changer and declarative schema changer will produce slightly
1516-
# different secondary index ordering for alter primary key, hence we assert
1517-
# separately with slightly different expected output.
1518-
onlyif config local-legacy-schema-changer
1519-
query T
1520-
SELECT create_statement FROM [SHOW CREATE TABLE t];
1521-
----
1522-
CREATE TABLE public.t (
1523-
a INT8 NOT NULL,
1524-
b INT8 NOT NULL,
1525-
k INT8 NOT NULL AS (a + b) VIRTUAL,
1526-
CONSTRAINT t_pkey PRIMARY KEY (a ASC),
1527-
UNIQUE INDEX t_b_k_key (b ASC, k ASC),
1528-
UNIQUE INDEX t_k_key (k ASC),
1529-
UNIQUE INDEX t_a_key (a ASC),
1530-
INDEX t_idx_b_k (b ASC, k ASC)
1531-
)
1532-
1533-
skipif config local-legacy-schema-changer
15341480
query T
15351481
SELECT create_statement FROM [SHOW CREATE TABLE t];
15361482
----
@@ -1950,22 +1896,6 @@ CREATE TABLE t_99303 (i INT NOT NULL PRIMARY KEY, j INT NOT NULL, UNIQUE INDEX (
19501896
statement ok
19511897
ALTER TABLE t_99303 ALTER PRIMARY KEY USING COLUMNS (j);
19521898

1953-
# Legacy schema changer and declarative schema changer will produce slightly
1954-
# different secondary index ordering for alter primary key, hence we assert
1955-
# separately with slightly different expected output.
1956-
onlyif config local-legacy-schema-changer
1957-
query TT
1958-
SHOW CREATE t_99303
1959-
----
1960-
t_99303 CREATE TABLE public.t_99303 (
1961-
i INT8 NOT NULL,
1962-
j INT8 NOT NULL,
1963-
CONSTRAINT t_99303_pkey PRIMARY KEY (j ASC),
1964-
UNIQUE INDEX t_99303_i_key1 (i ASC),
1965-
UNIQUE INDEX t_99303_i_key (i ASC) WHERE i > 0:::INT8
1966-
)
1967-
1968-
skipif config local-legacy-schema-changer
19691899
query TT
19701900
SHOW CREATE t_99303
19711901
----

pkg/sql/schema_changer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,8 +2257,8 @@ INSERT INTO t.test VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3);
22572257
y INT8 NOT NULL,
22582258
z INT8 NULL,
22592259
CONSTRAINT test_pkey PRIMARY KEY (y ASC),
2260-
UNIQUE INDEX test_x_key (x ASC),
2261-
INDEX i (z ASC)
2260+
INDEX i (z ASC),
2261+
UNIQUE INDEX test_x_key (x ASC)
22622262
)`
22632263
if create != expected {
22642264
t.Fatalf("expected %s, found %s", expected, create)

0 commit comments

Comments
 (0)