Skip to content

Commit 38ea6aa

Browse files
committed
Fix updates of indisvalid for partitioned indexes
indisvalid is switched to true for partitioned indexes when all its partitions have valid indexes when attaching a new partition, up to the top-most parent if all its leaves are themselves valid when dealing with multiple layers of partitions. The copy of the tuple from pg_index used to switch indisvalid to true came from the relation cache, which is incorrect. Particularly, in the case reported by Shruthi Gowda, executing a series of commands in a single transaction would cause the validation of partitioned indexes to use an incorrect version of a pg_index tuple, as indexes are reloaded after an invalidation request with RelationReloadIndexInfo(), a much faster version than a full index cache rebuild. In this case, the limited information updated in the cache leads to an incorrect version of the tuple used. One of the symptoms reported was the following error, with a replica identity update, for instance: "ERROR: attempted to update invisible tuple" This is incorrect since 8b08f7d, so backpatch all the way down. Reported-by: Shruthi Gowda Author: Michael Paquier Reviewed-by: Shruthi Gowda, Dilip Kumar Discussion: https://postgr.es/m/CAASxf_PBcxax0wW-3gErUyftZ0XrCs3Lrpuhq4-Z3Fak1DoW7Q@mail.gmail.com Backpatch-through: 11
1 parent d0c2860 commit 38ea6aa

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

src/backend/commands/tablecmds.c

+11-4
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
Relation idxRel;
19177-
HeapTuple newtup;
19177+
HeapTuple indTup;
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

+63
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

+43
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,46 @@ select indexrelid::regclass, indisvalid,
855855
where indexrelid::regclass::text like 'parted_isvalid%'
856856
order by indexrelid::regclass::text collate "C";
857857
drop table parted_isvalid_tab;
858+
859+
-- Check state of replica indexes when attaching a partition.
860+
begin;
861+
create table parted_replica_tab (id int not null) partition by range (id);
862+
create table parted_replica_tab_1 partition of parted_replica_tab
863+
for values from (1) to (10) partition by range (id);
864+
create table parted_replica_tab_11 partition of parted_replica_tab_1
865+
for values from (1) to (5);
866+
create unique index parted_replica_idx
867+
on only parted_replica_tab using btree (id);
868+
create unique index parted_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+
alter table only parted_replica_tab_1 replica identity
872+
using index parted_replica_idx_1;
873+
create unique index parted_replica_idx_11 on parted_replica_tab_11 USING btree (id);
874+
select indexrelid::regclass, indisvalid, indisreplident,
875+
indrelid::regclass, inhparent::regclass
876+
from pg_index idx left join
877+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
878+
where indexrelid::regclass::text like '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+
alter index parted_replica_idx ATTACH PARTITION parted_replica_idx_1;
883+
select indexrelid::regclass, indisvalid, indisreplident,
884+
indrelid::regclass, inhparent::regclass
885+
from pg_index idx left join
886+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
887+
where indexrelid::regclass::text like 'parted_replica%'
888+
order by indexrelid::regclass::text collate "C";
889+
-- parted_replica_idx becomes valid here.
890+
alter index parted_replica_idx_1 ATTACH PARTITION parted_replica_idx_11;
891+
alter table 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 idx left join
897+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
898+
where indexrelid::regclass::text like 'parted_replica%'
899+
order by indexrelid::regclass::text collate "C";
900+
drop table parted_replica_tab;

0 commit comments

Comments
 (0)