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

Commitf1d6bcd

Browse files
committed
Fix updates of indisvalid for partitioned indexes
indisvalid is switched to true for partitioned indexes when all itspartitions have valid indexes when attaching a new partition, up to thetop-most parent if all its leaves are themselves valid when dealing withmultiple layers of partitions.The copy of the tuple from pg_index used to switch indisvalid to truecame from the relation cache, which is incorrect. Particularly, in thecase reported by Shruthi Gowda, executing a series of commands in asingle transaction would cause the validation of partitioned indexes touse an incorrect version of a pg_index tuple, as indexes are reloadedafter an invalidation request with RelationReloadIndexInfo(), a muchfaster version than a full index cache rebuild. In this case, thelimited information updated in the cache leads to an incorrect versionof the tuple used. One of the symptoms reported was the followingerror, with a replica identity update, for instance:"ERROR: attempted to update invisible tuple"This is incorrect since8b08f7d, so backpatch all the way down.Reported-by: Shruthi GowdaAuthor: Michael PaquierReviewed-by: Shruthi Gowda, Dilip KumarDiscussion:https://postgr.es/m/CAASxf_PBcxax0wW-3gErUyftZ0XrCs3Lrpuhq4-Z3Fak1DoW7Q@mail.gmail.comBackpatch-through: 11
1 parent034a9fc commitf1d6bcd

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17490,17 +17490,24 @@ validatePartitionedIndex(Relation partedIdx, Relation partedTbl)
1749017490
if (tuples == RelationGetPartitionDesc(partedTbl)->nparts)
1749117491
{
1749217492
RelationidxRel;
17493-
HeapTuplenewtup;
17493+
HeapTupleindTup;
17494+
Form_pg_index indexForm;
1749417495

1749517496
idxRel = table_open(IndexRelationId, RowExclusiveLock);
17497+
indTup = SearchSysCacheCopy1(INDEXRELID,
17498+
ObjectIdGetDatum(RelationGetRelid(partedIdx)));
17499+
if (!HeapTupleIsValid(indTup))
17500+
elog(ERROR, "cache lookup failed for index %u",
17501+
RelationGetRelid(partedIdx));
17502+
indexForm = (Form_pg_index) GETSTRUCT(indTup);
1749617503

17497-
newtup = heap_copytuple(partedIdx->rd_indextuple);
17498-
((Form_pg_index) GETSTRUCT(newtup))->indisvalid = true;
17504+
indexForm->indisvalid = true;
1749917505
updated = true;
1750017506

17501-
CatalogTupleUpdate(idxRel, &partedIdx->rd_indextuple->t_self,newtup);
17507+
CatalogTupleUpdate(idxRel, &indTup->t_self,indTup);
1750217508

1750317509
table_close(idxRel, RowExclusiveLock);
17510+
heap_freetuple(indTup);
1750417511
}
1750517512

1750617513
/*

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,3 +1484,66 @@ select indexrelid::regclass, indisvalid,
14841484
(5 rows)
14851485

14861486
drop table parted_isvalid_tab;
1487+
-- Check state of replica indexes when attaching a partition.
1488+
begin;
1489+
create table parted_replica_tab (id int not null) partition by range (id);
1490+
create table parted_replica_tab_1 partition of parted_replica_tab
1491+
for values from (1) to (10) partition by range (id);
1492+
create table parted_replica_tab_11 partition of parted_replica_tab_1
1493+
for values from (1) to (5);
1494+
create unique index parted_replica_idx
1495+
on only parted_replica_tab using btree (id);
1496+
create unique index parted_replica_idx_1
1497+
on only parted_replica_tab_1 using btree (id);
1498+
-- This triggers an update of pg_index.indisreplident for parted_replica_idx.
1499+
alter table only parted_replica_tab_1 replica identity
1500+
using index parted_replica_idx_1;
1501+
create unique index parted_replica_idx_11 on parted_replica_tab_11 USING btree (id);
1502+
select indexrelid::regclass, indisvalid, indisreplident,
1503+
indrelid::regclass, inhparent::regclass
1504+
from pg_index idx left join
1505+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1506+
where indexrelid::regclass::text like 'parted_replica%'
1507+
order by indexrelid::regclass::text collate "C";
1508+
indexrelid | indisvalid | indisreplident | indrelid | inhparent
1509+
-----------------------+------------+----------------+-----------------------+-----------
1510+
parted_replica_idx | f | f | parted_replica_tab |
1511+
parted_replica_idx_1 | f | t | parted_replica_tab_1 |
1512+
parted_replica_idx_11 | t | f | parted_replica_tab_11 |
1513+
(3 rows)
1514+
1515+
-- parted_replica_idx is not valid yet here, because parted_replica_idx_1
1516+
-- is not valid.
1517+
alter index parted_replica_idx ATTACH PARTITION parted_replica_idx_1;
1518+
select indexrelid::regclass, indisvalid, indisreplident,
1519+
indrelid::regclass, inhparent::regclass
1520+
from pg_index idx left join
1521+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1522+
where indexrelid::regclass::text like 'parted_replica%'
1523+
order by indexrelid::regclass::text collate "C";
1524+
indexrelid | indisvalid | indisreplident | indrelid | inhparent
1525+
-----------------------+------------+----------------+-----------------------+--------------------
1526+
parted_replica_idx | f | f | parted_replica_tab |
1527+
parted_replica_idx_1 | f | t | parted_replica_tab_1 | parted_replica_idx
1528+
parted_replica_idx_11 | t | f | parted_replica_tab_11 |
1529+
(3 rows)
1530+
1531+
-- parted_replica_idx becomes valid here.
1532+
alter index parted_replica_idx_1 ATTACH PARTITION parted_replica_idx_11;
1533+
alter table only parted_replica_tab_1 replica identity
1534+
using index parted_replica_idx_1;
1535+
commit;
1536+
select indexrelid::regclass, indisvalid, indisreplident,
1537+
indrelid::regclass, inhparent::regclass
1538+
from pg_index idx left join
1539+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1540+
where indexrelid::regclass::text like 'parted_replica%'
1541+
order by indexrelid::regclass::text collate "C";
1542+
indexrelid | indisvalid | indisreplident | indrelid | inhparent
1543+
-----------------------+------------+----------------+-----------------------+----------------------
1544+
parted_replica_idx | t | f | parted_replica_tab |
1545+
parted_replica_idx_1 | t | t | parted_replica_tab_1 | parted_replica_idx
1546+
parted_replica_idx_11 | t | f | parted_replica_tab_11 | parted_replica_idx_1
1547+
(3 rows)
1548+
1549+
drop table parted_replica_tab;

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,3 +808,46 @@ select indexrelid::regclass, indisvalid,
808808
where indexrelid::regclass::textlike'parted_isvalid%'
809809
order by indexrelid::regclass::text collate"C";
810810
droptable parted_isvalid_tab;
811+
812+
-- Check state of replica indexes when attaching a partition.
813+
begin;
814+
createtableparted_replica_tab (idintnot null) partition by range (id);
815+
createtableparted_replica_tab_1 partition of parted_replica_tab
816+
forvaluesfrom (1) to (10) partition by range (id);
817+
createtableparted_replica_tab_11 partition of parted_replica_tab_1
818+
forvaluesfrom (1) to (5);
819+
createunique indexparted_replica_idx
820+
on only parted_replica_tab using btree (id);
821+
createunique indexparted_replica_idx_1
822+
on only parted_replica_tab_1 using btree (id);
823+
-- This triggers an update of pg_index.indisreplident for parted_replica_idx.
824+
altertable only parted_replica_tab_1 replica identity
825+
using index parted_replica_idx_1;
826+
createunique indexparted_replica_idx_11on parted_replica_tab_11 USING btree (id);
827+
select indexrelid::regclass, indisvalid, indisreplident,
828+
indrelid::regclass, inhparent::regclass
829+
from pg_index idxleft join
830+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
831+
where indexrelid::regclass::textlike'parted_replica%'
832+
order by indexrelid::regclass::text collate"C";
833+
-- parted_replica_idx is not valid yet here, because parted_replica_idx_1
834+
-- is not valid.
835+
alterindex parted_replica_idx ATTACH PARTITION parted_replica_idx_1;
836+
select indexrelid::regclass, indisvalid, indisreplident,
837+
indrelid::regclass, inhparent::regclass
838+
from pg_index idxleft join
839+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
840+
where indexrelid::regclass::textlike'parted_replica%'
841+
order by indexrelid::regclass::text collate"C";
842+
-- parted_replica_idx becomes valid here.
843+
alterindex parted_replica_idx_1 ATTACH PARTITION parted_replica_idx_11;
844+
altertable only parted_replica_tab_1 replica identity
845+
using index parted_replica_idx_1;
846+
commit;
847+
select indexrelid::regclass, indisvalid, indisreplident,
848+
indrelid::regclass, inhparent::regclass
849+
from pg_index idxleft join
850+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
851+
where indexrelid::regclass::textlike'parted_replica%'
852+
order by indexrelid::regclass::text collate"C";
853+
droptable parted_replica_tab;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp