Skip to content

Commit f1d6bcd

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 034a9fc commit f1d6bcd

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
Relation idxRel;
17493-
HeapTuple newtup;
17493+
HeapTuple indTup;
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::text like 'parted_isvalid%'
809809
order by indexrelid::regclass::text collate "C";
810810
drop table parted_isvalid_tab;
811+
812+
-- Check state of replica indexes when attaching a partition.
813+
begin;
814+
create table parted_replica_tab (id int not null) partition by range (id);
815+
create table parted_replica_tab_1 partition of parted_replica_tab
816+
for values from (1) to (10) partition by range (id);
817+
create table parted_replica_tab_11 partition of parted_replica_tab_1
818+
for values from (1) to (5);
819+
create unique index parted_replica_idx
820+
on only parted_replica_tab using btree (id);
821+
create unique index parted_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+
alter table only parted_replica_tab_1 replica identity
825+
using index parted_replica_idx_1;
826+
create unique index parted_replica_idx_11 on parted_replica_tab_11 USING btree (id);
827+
select indexrelid::regclass, indisvalid, indisreplident,
828+
indrelid::regclass, inhparent::regclass
829+
from pg_index idx left join
830+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
831+
where indexrelid::regclass::text like '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+
alter index parted_replica_idx ATTACH PARTITION parted_replica_idx_1;
836+
select indexrelid::regclass, indisvalid, indisreplident,
837+
indrelid::regclass, inhparent::regclass
838+
from pg_index idx left join
839+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
840+
where indexrelid::regclass::text like 'parted_replica%'
841+
order by indexrelid::regclass::text collate "C";
842+
-- parted_replica_idx becomes valid here.
843+
alter index parted_replica_idx_1 ATTACH PARTITION parted_replica_idx_11;
844+
alter table 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 idx left join
850+
pg_inherits inh on (idx.indexrelid = inh.inhrelid)
851+
where indexrelid::regclass::text like 'parted_replica%'
852+
order by indexrelid::regclass::text collate "C";
853+
drop table parted_replica_tab;

0 commit comments

Comments
 (0)