Skip to content

Commit 63b292e

Browse files
committed
Ignore invalid indexes when enforcing index rules in ALTER TABLE ATTACH PARTITION
A portion of ALTER TABLE .. ATTACH PARTITION is to ensure that the partition being attached to the partitioned table has a correct set of indexes, so as there is a consistent index mapping between the partitioned table and its new-to-be partition. However, as introduced in 8b08f7d, the current logic could choose an invalid index as a match, which is something that can exist when dealing with more than two levels of partitioning, like attaching a partitioned table (that has partitions, with an index created by CREATE INDEX ON ONLY) to another partitioned table. A partitioned index with indisvalid set to false is equivalent to an incomplete partition tree, meaning that an invalid partitioned index does not have indexes defined in all its partitions. Hence, choosing an invalid partitioned index can create inconsistent partition index trees, where the parent attaching to is valid, but its partition may be invalid. In the report from Alexander Lakhin, this showed up as an assertion failure when validating an index. Without assertions enabled, the partition index tree would be actually broken, as indisvalid should be switched to true for a partitioned index once all its partitions are themselves valid. With two levels of partitioning, the top partitioned table used a valid index and was able to link to an invalid index stored on its partition, itself a partitioned table. I have studied a few options here (like the possibility to switch indisvalid to false for the parent), but came down to the conclusion that we'd better rely on a simple rule: invalid indexes had better never be chosen, so as the partition attached uses and creates indexes that the parent expects. Some regression tests are added to provide some coverage. Note that the existing coverage is not impacted. This is a problem since partitioned indexes exist, so backpatch all the way down to v11. Reported-by: Alexander Lakhin Discussion: https://postgr.es/14987634-43c0-0cb3-e075-94d423607e08@gmail.com Backpatch-through: 11
1 parent 3b4580f commit 63b292e

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/backend/commands/tablecmds.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16626,8 +16626,8 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
1662616626

1662716627
/*
1662816628
* Scan the list of existing indexes in the partition-to-be, and mark
16629-
* the first matching, unattached one we find, if any, as partition of
16630-
* the parent index. If we find one, we're done.
16629+
* the first matching, valid, unattached one we find, if any, as
16630+
* partition of the parent index. If we find one, we're done.
1663116631
*/
1663216632
for (i = 0; i < list_length(attachRelIdxs); i++)
1663316633
{
@@ -16638,6 +16638,10 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
1663816638
if (attachrelIdxRels[i]->rd_rel->relispartition)
1663916639
continue;
1664016640

16641+
/* If this index is invalid, can't use it */
16642+
if (!attachrelIdxRels[i]->rd_index->indisvalid)
16643+
continue;
16644+
1664116645
if (CompareIndexInfo(attachInfos[i], info,
1664216646
attachrelIdxRels[i]->rd_indcollation,
1664316647
idxRel->rd_indcollation,

src/test/regress/expected/indexing.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,3 +1419,33 @@ Indexes:
14191419
"parted_index_col_drop11_b_idx" btree (b)
14201420

14211421
drop table parted_index_col_drop;
1422+
-- Check that invalid indexes are not selected when attaching a partition.
1423+
create table parted_inval_tab (a int) partition by range (a);
1424+
create index parted_inval_idx on parted_inval_tab (a);
1425+
create table parted_inval_tab_1 (a int) partition by range (a);
1426+
create table parted_inval_tab_1_1 partition of parted_inval_tab_1
1427+
for values from (0) to (10);
1428+
create table parted_inval_tab_1_2 partition of parted_inval_tab_1
1429+
for values from (10) to (20);
1430+
-- this creates an invalid index.
1431+
create index parted_inval_ixd_1 on only parted_inval_tab_1 (a);
1432+
-- this creates new indexes for all the partitions of parted_inval_tab_1,
1433+
-- discarding the invalid index created previously as what is chosen.
1434+
alter table parted_inval_tab attach partition parted_inval_tab_1
1435+
for values from (1) to (100);
1436+
select indexrelid::regclass, indisvalid,
1437+
indrelid::regclass, inhparent::regclass
1438+
from pg_index idx left join
1439+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1440+
where indexrelid::regclass::text like 'parted_inval%'
1441+
order by indexrelid::regclass::text collate "C";
1442+
indexrelid | indisvalid | indrelid | inhparent
1443+
----------------------------+------------+----------------------+--------------------------
1444+
parted_inval_idx | t | parted_inval_tab |
1445+
parted_inval_ixd_1 | f | parted_inval_tab_1 |
1446+
parted_inval_tab_1_1_a_idx | t | parted_inval_tab_1_1 | parted_inval_tab_1_a_idx
1447+
parted_inval_tab_1_2_a_idx | t | parted_inval_tab_1_2 | parted_inval_tab_1_a_idx
1448+
parted_inval_tab_1_a_idx | t | parted_inval_tab_1 | parted_inval_idx
1449+
(5 rows)
1450+
1451+
drop table parted_inval_tab;

src/test/regress/sql/indexing.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,25 @@ alter table parted_index_col_drop drop column c;
760760
\d parted_index_col_drop2
761761
\d parted_index_col_drop11
762762
drop table parted_index_col_drop;
763+
764+
-- Check that invalid indexes are not selected when attaching a partition.
765+
create table parted_inval_tab (a int) partition by range (a);
766+
create index parted_inval_idx on parted_inval_tab (a);
767+
create table parted_inval_tab_1 (a int) partition by range (a);
768+
create table parted_inval_tab_1_1 partition of parted_inval_tab_1
769+
for values from (0) to (10);
770+
create table parted_inval_tab_1_2 partition of parted_inval_tab_1
771+
for values from (10) to (20);
772+
-- this creates an invalid index.
773+
create index parted_inval_ixd_1 on only parted_inval_tab_1 (a);
774+
-- this creates new indexes for all the partitions of parted_inval_tab_1,
775+
-- discarding the invalid index created previously as what is chosen.
776+
alter table parted_inval_tab attach partition parted_inval_tab_1
777+
for values from (1) to (100);
778+
select indexrelid::regclass, indisvalid,
779+
indrelid::regclass, inhparent::regclass
780+
from pg_index idx left join
781+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
782+
where indexrelid::regclass::text like 'parted_inval%'
783+
order by indexrelid::regclass::text collate "C";
784+
drop table parted_inval_tab;

0 commit comments

Comments
 (0)