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

Commitcfc43ae

Browse files
committed
Fix marking of indisvalid for partitioned indexes at creation
The logic that introduced partitioned indexes missed a few things wheninvalidating a partitioned index when these are created, still the codeis written to handle recursions:1) If created from scratch because a mapping index could not be found,the new index created could be itself invalid, if for example it was apartitioned index with one of its leaves invalid.2) A CCI was missing when indisvalid is set for a parent index, leadingto inconsistent trees when recursing across more than one level for apartitioned index creation if an invalidation of the parent wasrequired.This could lead to the creation of a partition index tree where some ofthe partitioned indexes are marked as invalid, but some of the parentsare marked valid, which is not something that should happen (asvalidatePartitionedIndex() defines, indisvalid is switched to true for apartitioned index iff all its partitions are themselves valid).This patch makes sure that indisvalid is set to false on a partitionedindex if at least one of its partition is invalid. The flag is set totrue if *all* its partitions are valid.The regression test added in this commit abuses of a failed concurrentindex creation, marked as invalid, that maps with an index created onits partitioned table afterwards.Reported-by: Alexander LakhinReviewed-by: Alexander LakhinDiscussion:https://postgr.es/m/14987634-43c0-0cb3-e075-94d423607e08@gmail.comBackpatch-through: 11
1 parentc951e90 commitcfc43ae

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

‎src/backend/commands/indexcmds.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ DefineIndex(Oid relationId,
14161416
IndexStmt*childStmt=copyObject(stmt);
14171417
boolfound_whole_row;
14181418
ListCell*lc;
1419+
ObjectAddresschildAddr;
14191420

14201421
/*
14211422
* We can't use the same index name for the child index,
@@ -1469,15 +1470,25 @@ DefineIndex(Oid relationId,
14691470
Assert(GetUserId()==child_save_userid);
14701471
SetUserIdAndSecContext(root_save_userid,
14711472
root_save_sec_context);
1472-
DefineIndex(childRelid,childStmt,
1473-
InvalidOid,/* no predefined OID */
1474-
indexRelationId,/* this is our child */
1475-
createdConstraintId,
1476-
-1,
1477-
is_alter_table,check_rights,check_not_in_use,
1478-
skip_build,quiet);
1473+
childAddr=
1474+
DefineIndex(childRelid,childStmt,
1475+
InvalidOid,/* no predefined OID */
1476+
indexRelationId,/* this is our child */
1477+
createdConstraintId,
1478+
-1,
1479+
is_alter_table,check_rights,
1480+
check_not_in_use,
1481+
skip_build,quiet);
14791482
SetUserIdAndSecContext(child_save_userid,
14801483
child_save_sec_context);
1484+
1485+
/*
1486+
* Check if the index just created is valid or not, as it
1487+
* could be possible that it has been switched as invalid
1488+
* when recursing across multiple partition levels.
1489+
*/
1490+
if (!get_index_isvalid(childAddr.objectId))
1491+
invalidate_parent= true;
14811492
}
14821493

14831494
free_attrmap(attmap);
@@ -1507,6 +1518,12 @@ DefineIndex(Oid relationId,
15071518
ReleaseSysCache(tup);
15081519
table_close(pg_index,RowExclusiveLock);
15091520
heap_freetuple(newtup);
1521+
1522+
/*
1523+
* CCI here to make this update visible, in case this recurses
1524+
* across multiple partition levels.
1525+
*/
1526+
CommandCounterIncrement();
15101527
}
15111528
}
15121529

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,38 @@ select indexrelid::regclass, indisvalid,
14511451
(5 rows)
14521452

14531453
drop table parted_inval_tab;
1454+
-- Check setup of indisvalid across a complex partition tree on index
1455+
-- creation. If one index in a partition index is invalid, so should its
1456+
-- partitioned index.
1457+
create table parted_isvalid_tab (a int, b int) partition by range (a);
1458+
create table parted_isvalid_tab_1 partition of parted_isvalid_tab
1459+
for values from (1) to (10) partition by range (a);
1460+
create table parted_isvalid_tab_2 partition of parted_isvalid_tab
1461+
for values from (10) to (20) partition by range (a);
1462+
create table parted_isvalid_tab_11 partition of parted_isvalid_tab_1
1463+
for values from (1) to (5);
1464+
create table parted_isvalid_tab_12 partition of parted_isvalid_tab_1
1465+
for values from (5) to (10);
1466+
-- create an invalid index on one of the partitions.
1467+
insert into parted_isvalid_tab_11 values (1, 0);
1468+
create index concurrently parted_isvalid_idx_11 on parted_isvalid_tab_11 ((a/b));
1469+
ERROR: division by zero
1470+
-- The previous invalid index is selected, invalidating all the indexes up to
1471+
-- the top-most parent.
1472+
create index parted_isvalid_idx on parted_isvalid_tab ((a/b));
1473+
select indexrelid::regclass, indisvalid,
1474+
indrelid::regclass, inhparent::regclass
1475+
from pg_index idx left join
1476+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1477+
where indexrelid::regclass::text like 'parted_isvalid%'
1478+
order by indexrelid::regclass::text collate "C";
1479+
indexrelid | indisvalid | indrelid | inhparent
1480+
--------------------------------+------------+-----------------------+-------------------------------
1481+
parted_isvalid_idx | f | parted_isvalid_tab |
1482+
parted_isvalid_idx_11 | f | parted_isvalid_tab_11 | parted_isvalid_tab_1_expr_idx
1483+
parted_isvalid_tab_12_expr_idx | t | parted_isvalid_tab_12 | parted_isvalid_tab_1_expr_idx
1484+
parted_isvalid_tab_1_expr_idx | f | parted_isvalid_tab_1 | parted_isvalid_idx
1485+
parted_isvalid_tab_2_expr_idx | t | parted_isvalid_tab_2 | parted_isvalid_idx
1486+
(5 rows)
1487+
1488+
drop table parted_isvalid_tab;

‎src/test/regress/sql/indexing.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,3 +782,29 @@ select indexrelid::regclass, indisvalid,
782782
where indexrelid::regclass::textlike'parted_inval%'
783783
order by indexrelid::regclass::text collate"C";
784784
droptable parted_inval_tab;
785+
786+
-- Check setup of indisvalid across a complex partition tree on index
787+
-- creation. If one index in a partition index is invalid, so should its
788+
-- partitioned index.
789+
createtableparted_isvalid_tab (aint, bint) partition by range (a);
790+
createtableparted_isvalid_tab_1 partition of parted_isvalid_tab
791+
forvaluesfrom (1) to (10) partition by range (a);
792+
createtableparted_isvalid_tab_2 partition of parted_isvalid_tab
793+
forvaluesfrom (10) to (20) partition by range (a);
794+
createtableparted_isvalid_tab_11 partition of parted_isvalid_tab_1
795+
forvaluesfrom (1) to (5);
796+
createtableparted_isvalid_tab_12 partition of parted_isvalid_tab_1
797+
forvaluesfrom (5) to (10);
798+
-- create an invalid index on one of the partitions.
799+
insert into parted_isvalid_tab_11values (1,0);
800+
createindexconcurrently parted_isvalid_idx_11on parted_isvalid_tab_11 ((a/b));
801+
-- The previous invalid index is selected, invalidating all the indexes up to
802+
-- the top-most parent.
803+
createindexparted_isvalid_idxon parted_isvalid_tab ((a/b));
804+
select indexrelid::regclass, indisvalid,
805+
indrelid::regclass, inhparent::regclass
806+
from pg_index idxleft join
807+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
808+
where indexrelid::regclass::textlike'parted_isvalid%'
809+
order by indexrelid::regclass::text collate"C";
810+
droptable parted_isvalid_tab;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp