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

Commit38ea6aa

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 parentd0c2860 commit38ea6aa

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
@@ -19174,17 +19174,24 @@ validatePartitionedIndex(Relation partedIdx, Relation partedTbl)
1917419174
if (tuples == RelationGetPartitionDesc(partedTbl, true)->nparts)
1917519175
{
1917619176
RelationidxRel;
19177-
HeapTuplenewtup;
19177+
HeapTupleindTup;
19178+
Form_pg_index indexForm;
1917819179

1917919180
idxRel = table_open(IndexRelationId, RowExclusiveLock);
19181+
indTup = SearchSysCacheCopy1(INDEXRELID,
19182+
ObjectIdGetDatum(RelationGetRelid(partedIdx)));
19183+
if (!HeapTupleIsValid(indTup))
19184+
elog(ERROR, "cache lookup failed for index %u",
19185+
RelationGetRelid(partedIdx));
19186+
indexForm = (Form_pg_index) GETSTRUCT(indTup);
1918019187

19181-
newtup = heap_copytuple(partedIdx->rd_indextuple);
19182-
((Form_pg_index) GETSTRUCT(newtup))->indisvalid = true;
19188+
indexForm->indisvalid = true;
1918319189
updated = true;
1918419190

19185-
CatalogTupleUpdate(idxRel, &partedIdx->rd_indextuple->t_self,newtup);
19191+
CatalogTupleUpdate(idxRel, &indTup->t_self,indTup);
1918619192

1918719193
table_close(idxRel, RowExclusiveLock);
19194+
heap_freetuple(indTup);
1918819195
}
1918919196

1919019197
/*

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

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

15391539
drop table parted_isvalid_tab;
1540+
-- Check state of replica indexes when attaching a partition.
1541+
begin;
1542+
create table parted_replica_tab (id int not null) partition by range (id);
1543+
create table parted_replica_tab_1 partition of parted_replica_tab
1544+
for values from (1) to (10) partition by range (id);
1545+
create table parted_replica_tab_11 partition of parted_replica_tab_1
1546+
for values from (1) to (5);
1547+
create unique index parted_replica_idx
1548+
on only parted_replica_tab using btree (id);
1549+
create unique index parted_replica_idx_1
1550+
on only parted_replica_tab_1 using btree (id);
1551+
-- This triggers an update of pg_index.indisreplident for parted_replica_idx.
1552+
alter table only parted_replica_tab_1 replica identity
1553+
using index parted_replica_idx_1;
1554+
create unique index parted_replica_idx_11 on parted_replica_tab_11 USING btree (id);
1555+
select indexrelid::regclass, indisvalid, indisreplident,
1556+
indrelid::regclass, inhparent::regclass
1557+
from pg_index idx left join
1558+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1559+
where indexrelid::regclass::text like 'parted_replica%'
1560+
order by indexrelid::regclass::text collate "C";
1561+
indexrelid | indisvalid | indisreplident | indrelid | inhparent
1562+
-----------------------+------------+----------------+-----------------------+-----------
1563+
parted_replica_idx | f | f | parted_replica_tab |
1564+
parted_replica_idx_1 | f | t | parted_replica_tab_1 |
1565+
parted_replica_idx_11 | t | f | parted_replica_tab_11 |
1566+
(3 rows)
1567+
1568+
-- parted_replica_idx is not valid yet here, because parted_replica_idx_1
1569+
-- is not valid.
1570+
alter index parted_replica_idx ATTACH PARTITION parted_replica_idx_1;
1571+
select indexrelid::regclass, indisvalid, indisreplident,
1572+
indrelid::regclass, inhparent::regclass
1573+
from pg_index idx left join
1574+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1575+
where indexrelid::regclass::text like 'parted_replica%'
1576+
order by indexrelid::regclass::text collate "C";
1577+
indexrelid | indisvalid | indisreplident | indrelid | inhparent
1578+
-----------------------+------------+----------------+-----------------------+--------------------
1579+
parted_replica_idx | f | f | parted_replica_tab |
1580+
parted_replica_idx_1 | f | t | parted_replica_tab_1 | parted_replica_idx
1581+
parted_replica_idx_11 | t | f | parted_replica_tab_11 |
1582+
(3 rows)
1583+
1584+
-- parted_replica_idx becomes valid here.
1585+
alter index parted_replica_idx_1 ATTACH PARTITION parted_replica_idx_11;
1586+
alter table only parted_replica_tab_1 replica identity
1587+
using index parted_replica_idx_1;
1588+
commit;
1589+
select indexrelid::regclass, indisvalid, indisreplident,
1590+
indrelid::regclass, inhparent::regclass
1591+
from pg_index idx left join
1592+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
1593+
where indexrelid::regclass::text like 'parted_replica%'
1594+
order by indexrelid::regclass::text collate "C";
1595+
indexrelid | indisvalid | indisreplident | indrelid | inhparent
1596+
-----------------------+------------+----------------+-----------------------+----------------------
1597+
parted_replica_idx | t | f | parted_replica_tab |
1598+
parted_replica_idx_1 | t | t | parted_replica_tab_1 | parted_replica_idx
1599+
parted_replica_idx_11 | t | f | parted_replica_tab_11 | parted_replica_idx_1
1600+
(3 rows)
1601+
1602+
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
@@ -855,3 +855,46 @@ select indexrelid::regclass, indisvalid,
855855
where indexrelid::regclass::textlike'parted_isvalid%'
856856
order by indexrelid::regclass::text collate"C";
857857
droptable parted_isvalid_tab;
858+
859+
-- Check state of replica indexes when attaching a partition.
860+
begin;
861+
createtableparted_replica_tab (idintnot null) partition by range (id);
862+
createtableparted_replica_tab_1 partition of parted_replica_tab
863+
forvaluesfrom (1) to (10) partition by range (id);
864+
createtableparted_replica_tab_11 partition of parted_replica_tab_1
865+
forvaluesfrom (1) to (5);
866+
createunique indexparted_replica_idx
867+
on only parted_replica_tab using btree (id);
868+
createunique indexparted_replica_idx_1
869+
on only parted_replica_tab_1 using btree (id);
870+
-- This triggers an update of pg_index.indisreplident for parted_replica_idx.
871+
altertable only parted_replica_tab_1 replica identity
872+
using index parted_replica_idx_1;
873+
createunique indexparted_replica_idx_11on parted_replica_tab_11 USING btree (id);
874+
select indexrelid::regclass, indisvalid, indisreplident,
875+
indrelid::regclass, inhparent::regclass
876+
from pg_index idxleft join
877+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
878+
where indexrelid::regclass::textlike'parted_replica%'
879+
order by indexrelid::regclass::text collate"C";
880+
-- parted_replica_idx is not valid yet here, because parted_replica_idx_1
881+
-- is not valid.
882+
alterindex parted_replica_idx ATTACH PARTITION parted_replica_idx_1;
883+
select indexrelid::regclass, indisvalid, indisreplident,
884+
indrelid::regclass, inhparent::regclass
885+
from pg_index idxleft join
886+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
887+
where indexrelid::regclass::textlike'parted_replica%'
888+
order by indexrelid::regclass::text collate"C";
889+
-- parted_replica_idx becomes valid here.
890+
alterindex parted_replica_idx_1 ATTACH PARTITION parted_replica_idx_11;
891+
altertable only parted_replica_tab_1 replica identity
892+
using index parted_replica_idx_1;
893+
commit;
894+
select indexrelid::regclass, indisvalid, indisreplident,
895+
indrelid::regclass, inhparent::regclass
896+
from pg_index idxleft join
897+
pg_inherits inhon (idx.indexrelid=inh.inhrelid)
898+
where indexrelid::regclass::textlike'parted_replica%'
899+
order by indexrelid::regclass::text collate"C";
900+
droptable parted_replica_tab;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp