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

Commit663b35f

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 parent0789b82 commit663b35f

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

‎src/backend/commands/indexcmds.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,7 @@ DefineIndex(Oid relationId,
13741374
IndexStmt*childStmt=copyObject(stmt);
13751375
boolfound_whole_row;
13761376
ListCell*lc;
1377+
ObjectAddresschildAddr;
13771378

13781379
/*
13791380
* We can't use the same index name for the child index,
@@ -1427,14 +1428,24 @@ DefineIndex(Oid relationId,
14271428
Assert(GetUserId()==child_save_userid);
14281429
SetUserIdAndSecContext(root_save_userid,
14291430
root_save_sec_context);
1430-
DefineIndex(childRelid,childStmt,
1431-
InvalidOid,/* no predefined OID */
1432-
indexRelationId,/* this is our child */
1433-
createdConstraintId,
1434-
is_alter_table,check_rights,check_not_in_use,
1435-
skip_build,quiet);
1431+
childAddr=
1432+
DefineIndex(childRelid,childStmt,
1433+
InvalidOid,/* no predefined OID */
1434+
indexRelationId,/* this is our child */
1435+
createdConstraintId,
1436+
is_alter_table,check_rights,
1437+
check_not_in_use,
1438+
skip_build,quiet);
14361439
SetUserIdAndSecContext(child_save_userid,
14371440
child_save_sec_context);
1441+
1442+
/*
1443+
* Check if the index just created is valid or not, as it
1444+
* could be possible that it has been switched as invalid
1445+
* when recursing across multiple partition levels.
1446+
*/
1447+
if (!get_index_isvalid(childAddr.objectId))
1448+
invalidate_parent= true;
14381449
}
14391450

14401451
pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_DONE,
@@ -1466,6 +1477,12 @@ DefineIndex(Oid relationId,
14661477
ReleaseSysCache(tup);
14671478
table_close(pg_index,RowExclusiveLock);
14681479
heap_freetuple(newtup);
1480+
1481+
/*
1482+
* CCI here to make this update visible, in case this recurses
1483+
* across multiple partition levels.
1484+
*/
1485+
CommandCounterIncrement();
14691486
}
14701487
}
14711488

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

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

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