Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf428440

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 thepartition being attached to the partitioned table has a correct set ofindexes, so as there is a consistent index mapping between thepartitioned table and its new-to-be partition. However, as introducedin8b08f7d, the current logic could choose an invalid index as a match,which is something that can exist when dealing with more than two levelsof partitioning, like attaching a partitioned table (that haspartitions, with an index created by CREATE INDEX ON ONLY) to anotherpartitioned table.A partitioned index with indisvalid set to false is equivalent to anincomplete partition tree, meaning that an invalid partitioned indexdoes not have indexes defined in all its partitions. Hence, choosing aninvalid partitioned index can create inconsistent partition index trees,where the parent attaching to is valid, but its partition may beinvalid.In the report from Alexander Lakhin, this showed up as an assertionfailure when validating an index. Without assertions enabled, thepartition index tree would be actually broken, as indisvalid shouldbe switched to true for a partitioned index once all its partitions arethemselves valid. With two levels of partitioning, the top partitionedtable used a valid index and was able to link to an invalid index storedon its partition, itself a partitioned table.I have studied a few options here (like the possibility to switchindisvalid to false for the parent), but came down to the conclusionthat we'd better rely on a simple rule: invalid indexes had better neverbe chosen, so as the partition attached uses and creates indexes thatthe parent expects. Some regression tests are added to provide somecoverage. Note that the existing coverage is not impacted.This is a problem since partitioned indexes exist, so backpatch all theway down to v11.Reported-by: Alexander LakhinDiscussion:https://postgr.es/14987634-43c0-0cb3-e075-94d423607e08@gmail.comBackpatch-through: 11
1 parentb6ab18a commitf428440

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
@@ -17080,8 +17080,8 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
1708017080

1708117081
/*
1708217082
* Scan the list of existing indexes in the partition-to-be, and mark
17083-
* the first matching, unattached one we find, if any, as partition of
17084-
* the parent index. If we find one, we're done.
17083+
* the first matching,valid,unattached one we find, if any, as
17084+
*partition ofthe parent index. If we find one, we're done.
1708517085
*/
1708617086
for (i = 0; i < list_length(attachRelIdxs); i++)
1708717087
{
@@ -17092,6 +17092,10 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
1709217092
if (attachrelIdxRels[i]->rd_rel->relispartition)
1709317093
continue;
1709417094

17095+
/* If this index is invalid, can't use it */
17096+
if (!attachrelIdxRels[i]->rd_index->indisvalid)
17097+
continue;
17098+
1709517099
if (CompareIndexInfo(attachInfos[i], info,
1709617100
attachrelIdxRels[i]->rd_indcollation,
1709717101
idxRel->rd_indcollation,

‎src/test/regress/expected/indexing.out

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

14221422
drop table parted_index_col_drop;
1423+
-- Check that invalid indexes are not selected when attaching a partition.
1424+
create table parted_inval_tab (a int) partition by range (a);
1425+
create index parted_inval_idx on parted_inval_tab (a);
1426+
create table parted_inval_tab_1 (a int) partition by range (a);
1427+
create table parted_inval_tab_1_1 partition of parted_inval_tab_1
1428+
for values from (0) to (10);
1429+
create table parted_inval_tab_1_2 partition of parted_inval_tab_1
1430+
for values from (10) to (20);
1431+
-- this creates an invalid index.
1432+
create index parted_inval_ixd_1 on only parted_inval_tab_1 (a);
1433+
-- this creates new indexes for all the partitions of parted_inval_tab_1,
1434+
-- discarding the invalid index created previously as what is chosen.
1435+
alter table parted_inval_tab attach partition parted_inval_tab_1
1436+
for values from (1) to (100);
1437+
select indexrelid::regclass, indisvalid,
1438+
indrelid::regclass, inhparent::regclass
1439+
from pg_index idx left join
1440+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1441+
where indexrelid::regclass::text like 'parted_inval%'
1442+
order by indexrelid::regclass::text collate "C";
1443+
indexrelid | indisvalid | indrelid | inhparent
1444+
----------------------------+------------+----------------------+--------------------------
1445+
parted_inval_idx | t | parted_inval_tab |
1446+
parted_inval_ixd_1 | f | parted_inval_tab_1 |
1447+
parted_inval_tab_1_1_a_idx | t | parted_inval_tab_1_1 | parted_inval_tab_1_a_idx
1448+
parted_inval_tab_1_2_a_idx | t | parted_inval_tab_1_2 | parted_inval_tab_1_a_idx
1449+
parted_inval_tab_1_a_idx | t | parted_inval_tab_1 | parted_inval_idx
1450+
(5 rows)
1451+
1452+
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
droptable parted_index_col_drop;
763+
764+
-- Check that invalid indexes are not selected when attaching a partition.
765+
createtableparted_inval_tab (aint) partition by range (a);
766+
createindexparted_inval_idxon parted_inval_tab (a);
767+
createtableparted_inval_tab_1 (aint) partition by range (a);
768+
createtableparted_inval_tab_1_1 partition of parted_inval_tab_1
769+
forvaluesfrom (0) to (10);
770+
createtableparted_inval_tab_1_2 partition of parted_inval_tab_1
771+
forvaluesfrom (10) to (20);
772+
-- this creates an invalid index.
773+
createindexparted_inval_ixd_1on 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+
altertable parted_inval_tab attach partition parted_inval_tab_1
777+
forvaluesfrom (1) to (100);
778+
select indexrelid::regclass, indisvalid,
779+
indrelid::regclass, inhparent::regclass
780+
from pg_index idxleft join
781+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
782+
where indexrelid::regclass::textlike'parted_inval%'
783+
order by indexrelid::regclass::text collate"C";
784+
droptable parted_inval_tab;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp