Skip to content

Commit 07d4a96

Browse files
committed
sql: use existing PK index for inverted index valdiation
Previously, when we were validating inverted index, we made all first mutations public. For a PK swap operation this meant that incomplete primary indexes would be made public, which may not be fully ready for scans depending on the plan. This meant after recent changes to have better plans for alter primary key, we could end up scanning incomplete primary indexes. To address this, this patch ensures that only non-PK swap indexes are used for validating inverted indexes. Fixes: #155145 Fixes: #155141 Release note: None
1 parent 2c1b868 commit 07d4a96

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pkg/sql/backfill.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,8 +1785,10 @@ func countExpectedRowsForInvertedIndex(
17851785
if withFirstMutationPublic {
17861786
// Make the mutations public in an in-memory copy of the descriptor and
17871787
// add it to the Collection's synthetic descriptors, so that we can use
1788-
// SQL below to perform the validation.
1789-
fakeDesc, err := tableDesc.MakeFirstMutationPublic(catalog.IgnoreConstraints, catalog.RetainDroppingColumns)
1788+
// SQL below to perform the validation. Avoid making PK swaps public, otherwise
1789+
// in the declarative schema changer we can select incomplete primary indexes
1790+
// for validation.
1791+
fakeDesc, err := tableDesc.MakeFirstMutationPublic(catalog.IgnoreConstraints, catalog.RetainDroppingColumns, catalog.IgnorePKSwaps)
17901792
if err != nil {
17911793
return 0, err
17921794
}

pkg/sql/logictest/testdata/logic_test/alter_table

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5265,3 +5265,23 @@ statement ok
52655265
RESET experimental_enable_unique_without_index_constraints
52665266

52675267
subtest end
5268+
5269+
# Tests for a regression found by #155145 where the wrong primary index could
5270+
# be scanned for when validating inverted indexes recreated after an ALTER
5271+
# PRIMARY KEY.
5272+
subtest validate_alter_pk_with_inverted
5273+
5274+
statement ok
5275+
CREATE TABLE t_alter_pk_with_inverted(
5276+
a STRING NOT NULL,
5277+
d DECIMAL[],
5278+
INVERTED INDEX (d)
5279+
);
5280+
5281+
statement ok
5282+
INSERT INTO t_alter_pk_with_inverted VALUES ('test', ARRAY[1.0, 2.0]);
5283+
5284+
statement ok
5285+
ALTER TABLE t_alter_pk_with_inverted ALTER PRIMARY KEY USING COLUMNS (a) USING HASH;
5286+
5287+
subtest end

0 commit comments

Comments
 (0)