Skip to content

Commit

Permalink
Fix marking of indisvalid for partitioned indexes at creation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
michaelpq committed Jun 30, 2023
1 parent d0ab203 commit 537b70b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/backend/commands/indexcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ DefineIndex(Oid relationId,
IndexStmt *childStmt = copyObject(stmt);
bool found_whole_row;
ListCell *lc;
ObjectAddress childAddr;

/*
* We can't use the same index name for the child index,
Expand Down Expand Up @@ -1395,14 +1396,24 @@ DefineIndex(Oid relationId,
Assert(GetUserId() == child_save_userid);
SetUserIdAndSecContext(root_save_userid,
root_save_sec_context);
DefineIndex(childRelid, childStmt,
InvalidOid, /* no predefined OID */
indexRelationId, /* this is our child */
createdConstraintId,
is_alter_table, check_rights, check_not_in_use,
skip_build, quiet);
childAddr =
DefineIndex(childRelid, childStmt,
InvalidOid, /* no predefined OID */
indexRelationId, /* this is our child */
createdConstraintId,
is_alter_table, check_rights,
check_not_in_use,
skip_build, quiet);
SetUserIdAndSecContext(child_save_userid,
child_save_sec_context);

/*
* Check if the index just created is valid or not, as it
* could be possible that it has been switched as invalid
* when recursing across multiple partition levels.
*/
if (!get_index_isvalid(childAddr.objectId))
invalidate_parent = true;
}

pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_DONE,
Expand Down Expand Up @@ -1434,6 +1445,12 @@ DefineIndex(Oid relationId,
ReleaseSysCache(tup);
table_close(pg_index, RowExclusiveLock);
heap_freetuple(newtup);

/*
* CCI here to make this update visible, in case this recurses
* across multiple partition levels.
*/
CommandCounterIncrement();
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/test/regress/expected/indexing.out
Original file line number Diff line number Diff line change
Expand Up @@ -1450,3 +1450,38 @@ select indexrelid::regclass, indisvalid,
(5 rows)

drop table parted_inval_tab;
-- Check setup of indisvalid across a complex partition tree on index
-- creation. If one index in a partition index is invalid, so should its
-- partitioned index.
create table parted_isvalid_tab (a int, b int) partition by range (a);
create table parted_isvalid_tab_1 partition of parted_isvalid_tab
for values from (1) to (10) partition by range (a);
create table parted_isvalid_tab_2 partition of parted_isvalid_tab
for values from (10) to (20) partition by range (a);
create table parted_isvalid_tab_11 partition of parted_isvalid_tab_1
for values from (1) to (5);
create table parted_isvalid_tab_12 partition of parted_isvalid_tab_1
for values from (5) to (10);
-- create an invalid index on one of the partitions.
insert into parted_isvalid_tab_11 values (1, 0);
create index concurrently parted_isvalid_idx_11 on parted_isvalid_tab_11 ((a/b));
ERROR: division by zero
-- The previous invalid index is selected, invalidating all the indexes up to
-- the top-most parent.
create index parted_isvalid_idx on parted_isvalid_tab ((a/b));
select indexrelid::regclass, indisvalid,
indrelid::regclass, inhparent::regclass
from pg_index idx left join
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
where indexrelid::regclass::text like 'parted_isvalid%'
order by indexrelid::regclass::text collate "C";
indexrelid | indisvalid | indrelid | inhparent
--------------------------------+------------+-----------------------+-------------------------------
parted_isvalid_idx | f | parted_isvalid_tab |
parted_isvalid_idx_11 | f | parted_isvalid_tab_11 | parted_isvalid_tab_1_expr_idx
parted_isvalid_tab_12_expr_idx | t | parted_isvalid_tab_12 | parted_isvalid_tab_1_expr_idx
parted_isvalid_tab_1_expr_idx | f | parted_isvalid_tab_1 | parted_isvalid_idx
parted_isvalid_tab_2_expr_idx | t | parted_isvalid_tab_2 | parted_isvalid_idx
(5 rows)

drop table parted_isvalid_tab;
26 changes: 26 additions & 0 deletions src/test/regress/sql/indexing.sql
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,29 @@ select indexrelid::regclass, indisvalid,
where indexrelid::regclass::text like 'parted_inval%'
order by indexrelid::regclass::text collate "C";
drop table parted_inval_tab;

-- Check setup of indisvalid across a complex partition tree on index
-- creation. If one index in a partition index is invalid, so should its
-- partitioned index.
create table parted_isvalid_tab (a int, b int) partition by range (a);
create table parted_isvalid_tab_1 partition of parted_isvalid_tab
for values from (1) to (10) partition by range (a);
create table parted_isvalid_tab_2 partition of parted_isvalid_tab
for values from (10) to (20) partition by range (a);
create table parted_isvalid_tab_11 partition of parted_isvalid_tab_1
for values from (1) to (5);
create table parted_isvalid_tab_12 partition of parted_isvalid_tab_1
for values from (5) to (10);
-- create an invalid index on one of the partitions.
insert into parted_isvalid_tab_11 values (1, 0);
create index concurrently parted_isvalid_idx_11 on parted_isvalid_tab_11 ((a/b));
-- The previous invalid index is selected, invalidating all the indexes up to
-- the top-most parent.
create index parted_isvalid_idx on parted_isvalid_tab ((a/b));
select indexrelid::regclass, indisvalid,
indrelid::regclass, inhparent::regclass
from pg_index idx left join
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
where indexrelid::regclass::text like 'parted_isvalid%'
order by indexrelid::regclass::text collate "C";
drop table parted_isvalid_tab;

0 comments on commit 537b70b

Please sign in to comment.