8000 Fix marking of indisvalid for partitioned indexes at creation · postgres/postgres@c75c33d · GitHub
[go: up one dir, main page]

Skip to content

Commit c75c33d

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 7f11b7a commit c75c33d

File tree

5 files changed

+108
-6
lines changed

5 files changed

+108
-6
lines changed

src/backend/commands/indexcmds.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,7 @@ DefineIndex(Oid relationId,
11601160
IndexStmt *childStmt = copyObject(stmt);
11611161
bool found_whole_row;
11621162
ListCell *lc;
1163+
ObjectAddress childAddr;
11631164

11641165
/*
11651166
* We can't use the same index name for the child index,
@@ -1212,14 +1213,24 @@ DefineIndex(Oid relationId,
12121213
Assert(GetUserId() == child_save_userid);
12131214
SetUserIdAndSecContext(root_save_userid,
12141215
root_save_sec_context);
1215-
DefineIndex(childRelid, childStmt,
1216-
InvalidOid, /* no predefined OID */
1217-
indexRelationId, /* this is our child */
1218-
createdConstraintId,
1219-
is_alter_table, check_rights, check_not_in_use,
1220-
skip_build, quiet);
1216+
childAddr =
1217+
DefineIndex(childRelid, childStmt,
1218+
InvalidOid, /* no predefined OID */
1219+
indexRelationId, /* this is our child */
1220+
createdConstraintId,
1221+
is_alter_table, check_rights,
1222+
check_not_in_use,
1223+
skip_build, quiet);
12211224
SetUserIdAndSecContext(child_save_userid,
12221225
child_save_sec_context);
1226+
1227+
/*
1228+
* Check if the index just created is valid or not, as it
1229+
* could be possible that it has been switched as invalid
1230+
* when recursing across multiple partition levels.
1231+
*/
1232+
if (!get_index_isvalid(childAddr.objectId))
1233+
invalidate_parent = true;
12231234
}
12241235

12251236
pfree(attmap);
@@ -1249,6 +1260,12 @@ DefineIndex(Oid relationId,
12491260
ReleaseSysCache(tup);
12501261
heap_close(pg_index, RowExclusiveLock);
12511262
heap_freetuple(newtup);
1263+
1264+
/*
1265+
* CCI here to make this update visible, in case this recurses
1266+
* across multiple partition levels.
1267+
*/
1268+
CommandCounterIncrement();
12521269
}
12531270
}
12541271

src/backend/utils/cache/lsyscache.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,3 +3204,26 @@ get_index_isclustered(Oid index_oid)
32043204

32053205
return isclustered;
32063206
}
3207+
3208+
/*
3209+
* get_index_isvalid
3210+
*
3211+
* Given the index OID, return pg_index.indisvalid.
3212+
*/
3213+
bool
3214+
get_index_isvalid(Oid index_oid)
3215+
{
3216+
bool isvalid;
3217+
HeapTuple tuple;
3218+
Form_pg_index rd_index;
3219+
3220+
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
3221+
if (!HeapTupleIsValid(tuple))
3222+
elog(ERROR, "cache lookup failed for index %u", index_oid);
3223+
3224+
rd_index = (Form_pg_index) GETSTRUCT(tuple);
3225+
isvalid = rd_index->indisvalid;
3226+
ReleaseSysCache(tuple);
3227+
3228+
return isvalid;
3229+
}

src/include/utils/lsyscache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ extern char *get_namespace_name_or_temp(Oid nspid);
179179
extern Oid get_range_subtype(Oid rangeOid);
180180
extern Oid get_range_collation(Oid rangeOid);
181181
extern bool get_index_isreplident(Oid index_oid);
182+
extern bool get_index_isvalid(Oid index_oid);
182183
extern bool get_index_isclustered(Oid index_oid);
183184

184185
#define type_is_array(typid) (get_element_type(typid) != InvalidOid)

src/test/regress/expected/indexing.out

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

15391539
drop table parted_inval_tab;
1540+
-- Check setup of indisvalid across a complex partition tree on index
1541+
-- creation. If one index in a partition index is invalid, so should its
1542+
-- partitioned index.
1543+
create table parted_isvalid_tab (a int, b int) partition by range (a);
1544+
create table parted_isvalid_tab_1 partition of parted_isvalid_tab
1545+
for values from (1) to (10) partition by range (a);
1546+
create table parted_isvalid_tab_2 partition of parted_isvalid_tab
1547+
for values from (10) to (20) partition by range (a);
1548+
create table parted_isvalid_tab_11 partition of parted_isvalid_tab_1
1549+
for values from (1) to (5);
1550+
create table parted_isvalid_tab_12 partition of parted_isvalid_tab_1
1551+
for values from (5) to (10);
1552+
-- create an invalid index on one of the partitions.
1553+
insert into parted_isvalid_tab_11 values (1, 0);
1554+
create index concurrently parted_isvalid_idx_11 on parted_isvalid_tab_11 ((a/b));
1555+
ERROR: division by zero
1556+
-- The previous invalid index is selected, invalidating all the indexes up to
1557+
-- the top-most parent.
1558+
create index parted_isvalid_idx on parted_isvalid_tab ((a/b));
1559+
select indexrelid::regclass, indisvalid,
1560+
indrelid::regclass, inhparent::regclass
1561+
from pg_index idx left join
1562+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1563+
where indexrelid::regclass::text like 'parted_isvalid%'
1564+
order by indexrelid::regclass::text collate "C";
1565+
indexrelid | indisvalid | indrelid | inhparent
1566+
--------------------------------+------------+-----------------------+-------------------------------
1567+
parted_isvalid_idx | f | parted_isvalid_tab |
1568+
parted_isvalid_idx_11 | f | parted_isvalid_tab_11 | parted_isvalid_tab_1_expr_idx
1569+
parted_isvalid_tab_12_expr_idx | t | parted_isvalid_tab_12 | parted_isvalid_tab_1_expr_idx
1570+
parted_isvalid_tab_1_expr_idx | f | parted_isvalid_tab_1 | parted_isvalid_idx
1571+
parted_isvalid_tab_2_expr_idx | t | parted_isvalid_tab_2 | parted_isvalid_idx
1572+
(5 rows)
1573+
1574+
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
@@ -817,3 +817,29 @@ select indexrelid::regclass, indisvalid,
817817
where indexrelid::regclass::text like 'parted_inval%'
818818
order by indexrelid::regclass::text collate "C";
819819
drop table parted_inval_tab;
820+
821+
-- Check setup of indisvalid across a complex partition tree on index
822+
-- creation. If one index in a partition index is invalid, so should its
823+
-- partitioned index.
824+
create table parted_isvalid_tab (a int, b int) partition by range (a);
825+
create table parted_isvalid_tab_1 partition of parted_isvalid_tab
826+
for values from (1) to (10) partition by range (a);
827+
create table parted_isvalid_tab_2 partition of parted_isvalid_tab
828+
for values from (10) to (20) partition by range (a);
829+
create table parted_isvalid_tab_11 partition of parted_isvalid_tab_1
830+
for values from (1) to (5);
831+
create table parted_isvalid_tab_12 partition of parted_isvalid_tab_1
832+
for values from (5) to (10);
833+
-- create an invalid index on one of the partitions.
834+
insert into parted_isvalid_tab_11 values (1, 0);
835+
create index concurrently parted_isvalid_idx_11 on parted_isvalid_tab_11 ((a/b));
836+
-- The previous invalid index is selected, invalidating all the indexes up to
837+
-- the top-most parent.
838+
create index parted_isvalid_idx on parted_isvalid_tab ((a/b));
839+
select indexrelid::regclass, indisvalid,
840+
indrelid::regclass, inhparent::regclass
841+
from pg_index idx left join
842+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
843+
where indexrelid::regclass::text like 'parted_isvalid%'
844+
order by indexrelid::regclass::text collate "C";
845+
drop table parted_isvalid_tab;

0 commit comments

Comments
 (0)
0