Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cfc43ae

Browse files
committed
Fix marking of indisvalid for partitioned indexes at creation
The logic that introduced partitioned indexes missed a few things when invalidating a partitioned index when these are created, still the code is 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 a partitioned index with one of its leaves invalid. 2) A CCI was missing when indisvalid is set for a parent index, leading to inconsistent trees when recursing across more than one level for a partitioned index creation if an invalidation of the parent was required. This could lead to the creation of a partition index tree where some of the partitioned indexes are marked as invalid, but some of the parents are marked valid, which is not something that should happen (as validatePartitionedIndex() defines, indisvalid is switched to true for a partitioned index iff all its partitions are themselves valid). This patch makes sure that indisvalid is set to false on a partitioned index if at least one of its partition is invalid. The flag is set to true if *all* its partitions are valid. The regression test added in this commit abuses of a failed concurrent index creation, marked as invalid, that maps with an index created on its partitioned table afterwards. Reported-by: Alexander Lakhin Reviewed-by: Alexander Lakhin Discussion: https://postgr.es/m/14987634-43c0-0cb3-e075-94d423607e08@gmail.com Backpatch-through: 11
1 parent c951e90 commit cfc43ae

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
bool found_whole_row;
14181418
ListCell *lc;
1419+
ObjectAddress childAddr;
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::text like 'parted_inval%'
783783
order by indexrelid::regclass::text collate "C";
784784
drop table 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+
create table parted_isvalid_tab (a int, b int) partition by range (a);
790+
create table parted_isvalid_tab_1 partition of parted_isvalid_tab
791+
for values from (1) to (10) partition by range (a);
792+
create table parted_isvalid_tab_2 partition of parted_isvalid_tab
793+
for values from (10) to (20) partition by range (a);
794+
create table parted_isvalid_tab_11 partition of parted_isvalid_tab_1
795+
for values from (1) to (5);
796+
create table parted_isvalid_tab_12 partition of parted_isvalid_tab_1
797+
for values from (5) to (10);
798+
-- create an invalid index on one of the partitions.
799+
insert into parted_isvalid_tab_11 values (1, 0);
800+
create index concurrently parted_isvalid_idx_11 on 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+
create index parted_isvalid_idx on parted_isvalid_tab ((a/b));
804+
select indexrelid::regclass, indisvalid,
805+
indrelid::regclass, inhparent::regclass
806+
from pg_index idx left join
807+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
808+
where indexrelid::regclass::text like 'parted_isvalid%'
809+
order by indexrelid::regclass::text collate "C";
810+
drop table parted_isvalid_tab;

0 commit comments

Comments
 (0)