Skip to content

Commit c9f7f92

Browse files
committed
Allow REPLICA IDENTITY to be set on an index that's not (yet) valid.
The motivation for this change is that when pg_dump dumps a partitioned index that's marked REPLICA IDENTITY, it generates a command sequence that applies REPLICA IDENTITY before the partitioned index has been marked valid, causing restore to fail. We could perhaps change pg_dump to not do it like that, but that would be difficult and would not fix existing dump files with the problem. There seems to be very little reason for the backend to disallow this anyway --- the code ignores indisreplident when the index isn't valid --- so instead let's fix it by allowing the case. Commit 9511fb3 previously expressed a concern that allowing indisreplident to be set on invalid indexes might allow us to wind up in a situation where a table could have indisreplident set on multiple indexes. I'm not sure I follow that concern exactly, but in any case the only way that could happen is because relation_mark_replica_identity is too trusting about the existing set of markings being valid. Let's just rip out its early-exit code path (which sure looks like premature optimization anyway; what are we doing expending code to make redundant ALTER TABLE ... REPLICA IDENTITY commands marginally faster and not-redundant ones marginally slower?) and fix it to positively guarantee that no more than one index is marked indisreplident. The pg_dump failure can be demonstrated in all supported branches, so back-patch all the way. I chose to back-patch 9511fb3 as well, just to keep indisreplident handling the same in all branches. Per bug #17756 from Sergey Belyashov. Discussion: https://postgr.es/m/17756-dd50e8e0c8dd4a40@postgresql.org
1 parent e52daaa commit c9f7f92

File tree

4 files changed

+81
-45
lines changed

4 files changed

+81
-45
lines changed

src/backend/catalog/index.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,9 +3482,8 @@ index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
34823482
* CONCURRENTLY that failed partway through.)
34833483
*
34843484
* Note: the CLUSTER logic assumes that indisclustered cannot be
3485-
* set on any invalid index, so clear that flag too. Similarly,
3486-
* ALTER TABLE assumes that indisreplident cannot be set for
3487-
* invalid indexes.
3485+
* set on any invalid index, so clear that flag too. For
3486+
* cleanliness, also clear indisreplident.
34883487
*/
34893488
indexForm->indisvalid = false;
34903489
indexForm->indisclustered = false;

src/backend/commands/tablecmds.c

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15773,7 +15773,10 @@ ATExecDropOf(Relation rel, LOCKMODE lockmode)
1577315773
* relation_mark_replica_identity: Update a table's replica identity
1577415774
*
1577515775
* Iff ri_type = REPLICA_IDENTITY_INDEX, indexOid must be the Oid of a suitable
15776-
* index. Otherwise, it should be InvalidOid.
15776+
* index. Otherwise, it must be InvalidOid.
15777+
*
15778+
* Caller had better hold an exclusive lock on the relation, as the results
15779+
* of running two of these concurrently wouldn't be pretty.
1577715780
*/
1577815781
static void
1577915782
relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
@@ -15785,7 +15788,6 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
1578515788
HeapTuple pg_index_tuple;
1578615789
Form_pg_class pg_class_form;
1578715790
Form_pg_index pg_index_form;
15788-
1578915791
ListCell *index;
1579015792

1579115793
/*
@@ -15807,29 +15809,7 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
1580715809
heap_freetuple(pg_class_tuple);
1580815810

1580915811
/*
15810-
* Check whether the correct index is marked indisreplident; if so, we're
15811-
* done.
15812-
*/
15813-
if (OidIsValid(indexOid))
15814-
{
15815-
Assert(ri_type == REPLICA_IDENTITY_INDEX);
15816-
15817-
pg_index_tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexOid));
15818-
if (!HeapTupleIsValid(pg_index_tuple))
15819-
elog(ERROR, "cache lookup failed for index %u", indexOid);
15820-
pg_index_form = (Form_pg_index) GETSTRUCT(pg_index_tuple);
15821-
15822-
if (pg_index_form->indisreplident)
15823-
{
15824-
ReleaseSysCache(pg_index_tuple);
15825-
return;
15826-
}
15827-
ReleaseSysCache(pg_index_tuple);
15828-
}
15829-
15830-
/*
15831-
* Clear the indisreplident flag from any index that had it previously,
15832-
* and set it for any index that should have it now.
15812+
* Update the per-index indisreplident flags correctly.
1583315813
*/
1583415814
pg_index = table_open(IndexRelationId, RowExclusiveLock);
1583515815
foreach(index, RelationGetIndexList(rel))
@@ -15843,19 +15823,23 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
1584315823
elog(ERROR, "cache lookup failed for index %u", thisIndexOid);
1584415824
pg_index_form = (Form_pg_index) GETSTRUCT(pg_index_tuple);
1584515825

15846-
/*
15847-
* Unset the bit if set. We know it's wrong because we checked this
15848-
* earlier.
15849-
*/
15850-
if (pg_index_form->indisreplident)
15826+
if (thisIndexOid == indexOid)
1585115827
{
15852-
dirty = true;
15853-
pg_index_form->indisreplident = false;
15828+
/* Set the bit if not already set. */
15829+
if (!pg_index_form->indisreplident)
15830+
{
15831+
dirty = true;
15832+
pg_index_form->indisreplident = true;
15833+
}
1585415834
}
15855-
else if (thisIndexOid == indexOid)
15835+
else
1585615836
{
15857-
dirty = true;
15858-
pg_index_form->indisreplident = true;
15837+
/* Unset the bit if set. */
15838+
if (pg_index_form->indisreplident)
15839+
{
15840+
dirty = true;
15841+
pg_index_form->indisreplident = false;
15842+
}
1585915843
}
1586015844

1586115845
if (dirty)
@@ -15867,7 +15851,9 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
1586715851
/*
1586815852
* Invalidate the relcache for the table, so that after we commit
1586915853
* all sessions will refresh the table's replica identity index
15870-
* before attempting any UPDATE or DELETE on the table.
15854+
* before attempting any UPDATE or DELETE on the table. (If we
15855+
* changed the table's pg_class row above, then a relcache inval
15856+
* is already queued due to that; but we might not have.)
1587115857
*/
1587215858
CacheInvalidateRelcache(rel);
1587315859
}
@@ -15952,12 +15938,6 @@ ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode
1595215938
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1595315939
errmsg("cannot use partial index \"%s\" as replica identity",
1595415940
RelationGetRelationName(indexRel))));
15955-
/* And neither are invalid indexes. */
15956-
if (!indexRel->rd_index->indisvalid)
15957-
ereport(ERROR,
15958-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
15959-
errmsg("cannot use invalid index \"%s\" as replica identity",
15960-
RelationGetRelationName(indexRel))));
1596115941

1596215942
/* Check index for nullable columns. */
1596315943
for (key = 0; key < IndexRelationGetNumberOfKeyAttributes(indexRel); key++)

src/test/regress/expected/replica_identity.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,44 @@ Indexes:
227227
-- used as replica identity.
228228
ALTER TABLE test_replica_identity3 ALTER COLUMN id DROP NOT NULL;
229229
ERROR: column "id" is in index used as replica identity
230+
--
231+
-- Test that replica identity can be set on an index that's not yet valid.
232+
-- (This matches the way pg_dump will try to dump a partitioned table.)
233+
--
234+
CREATE TABLE test_replica_identity4(id integer NOT NULL) PARTITION BY LIST (id);
235+
CREATE TABLE test_replica_identity4_1(id integer NOT NULL);
236+
ALTER TABLE ONLY test_replica_identity4
237+
ATTACH PARTITION test_replica_identity4_1 FOR VALUES IN (1);
238+
ALTER TABLE ONLY test_replica_identity4
239+
ADD CONSTRAINT test_replica_identity4_pkey PRIMARY KEY (id);
240+
ALTER TABLE ONLY test_replica_identity4
241+
REPLICA IDENTITY USING INDEX test_replica_identity4_pkey;
242+
ALTER TABLE ONLY test_replica_identity4_1
243+
ADD CONSTRAINT test_replica_identity4_1_pkey PRIMARY KEY (id);
244+
\d+ test_replica_identity4
245+
Partitioned table "public.test_replica_identity4"
246+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
247+
--------+---------+-----------+----------+---------+---------+--------------+-------------
248+
id | integer | | not null | | plain | |
249+
Partition key: LIST (id)
250+
Indexes:
251+
"test_replica_identity4_pkey" PRIMARY KEY, btree (id) INVALID REPLICA IDENTITY
252+
Partitions: test_replica_identity4_1 FOR VALUES IN (1)
253+
254+
ALTER INDEX test_replica_identity4_pkey
255+
ATTACH PARTITION test_replica_identity4_1_pkey;
256+
\d+ test_replica_identity4
257+
Partitioned table "public.test_replica_identity4"
258+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
259+
--------+---------+-----------+----------+---------+---------+--------------+-------------
260+
id | integer | | not null | | plain | |
261+
Partition key: LIST (id)
262+
Indexes:
263+
"test_replica_identity4_pkey" PRIMARY KEY, btree (id) REPLICA IDENTITY
264+
Partitions: test_replica_identity4_1 FOR VALUES IN (1)
265+
230266
DROP TABLE test_replica_identity;
231267
DROP TABLE test_replica_identity2;
232268
DROP TABLE test_replica_identity3;
269+
DROP TABLE test_replica_identity4;
233270
DROP TABLE test_replica_identity_othertable;

src/test/regress/sql/replica_identity.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,27 @@ ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
9898
-- used as replica identity.
9999
ALTER TABLE test_replica_identity3 ALTER COLUMN id DROP NOT NULL;
100100

101+
--
102+
-- Test that replica identity can be set on an index that's not yet valid.
103+
-- (This matches the way pg_dump will try to dump a partitioned table.)
104+
--
105+
CREATE TABLE test_replica_identity4(id integer NOT NULL) PARTITION BY LIST (id);
106+
CREATE TABLE test_replica_identity4_1(id integer NOT NULL);
107+
ALTER TABLE ONLY test_replica_identity4
108+
ATTACH PARTITION test_replica_identity4_1 FOR VALUES IN (1);
109+
ALTER TABLE ONLY test_replica_identity4
110+
ADD CONSTRAINT test_replica_identity4_pkey PRIMARY KEY (id);
111+
ALTER TABLE ONLY test_replica_identity4
112+
REPLICA IDENTITY USING INDEX test_replica_identity4_pkey;
113+
ALTER TABLE ONLY test_replica_identity4_1
114+
ADD CONSTRAINT test_replica_identity4_1_pkey PRIMARY KEY (id);
115+
\d+ test_replica_identity4
116+
ALTER INDEX test_replica_identity4_pkey
117+
ATTACH PARTITION test_replica_identity4_1_pkey;
118+
\d+ test_replica_identity4
119+
101120
DROP TABLE test_replica_identity;
102121
DROP TABLE test_replica_identity2;
103122
DROP TABLE test_replica_identity3;
123+
DROP TABLE test_replica_identity4;
104124
DROP TABLE test_replica_identity_othertable;

0 commit comments

Comments
 (0)