Skip to content

Commit e2d53c8

Browse files
committed
Flush table's relcache during ALTER TABLE ADD PRIMARY KEY USING INDEX.
Previously, unless we had to add a NOT NULL constraint to the column, this command resulted in updating only the index's relcache entry. That's problematic when replication behavior is being driven off the existence of a primary key: other sessions (and ours too for that matter) failed to recalculate their opinion of whether the table can be replicated. Add a relcache invalidation to fix it. This has been broken since pg_class.relhaspkey was removed in v11. Before that, updating the table's relhaspkey value sufficed to cause a cache flush. Hence, backpatch to v11. Report and patch by Hou Zhijie Discussion: https://postgr.es/m/OS0PR01MB5716EBE01F112C62F8F9B786947B9@OS0PR01MB5716.jpnprd01.prod.outlook.com
1 parent 5521d81 commit e2d53c8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/backend/catalog/index.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,7 @@ index_constraint_create(Relation heapRelation,
20252025
HeapTuple indexTuple;
20262026
Form_pg_index indexForm;
20272027
bool dirty = false;
2028+
bool marked_as_primary = false;
20282029

20292030
pg_index = table_open(IndexRelationId, RowExclusiveLock);
20302031

@@ -2038,6 +2039,7 @@ index_constraint_create(Relation heapRelation,
20382039
{
20392040
indexForm->indisprimary = true;
20402041
dirty = true;
2042+
marked_as_primary = true;
20412043
}
20422044

20432045
if (deferrable && indexForm->indimmediate)
@@ -2050,6 +2052,15 @@ index_constraint_create(Relation heapRelation,
20502052
{
20512053
CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
20522054

2055+
/*
2056+
* When we mark an existing index as primary, force a relcache
2057+
* flush on its parent table, so that all sessions will become
2058+
* aware that the table now has a primary key. This is important
2059+
* because it affects some replication behaviors.
2060+
*/
2061+
if (marked_as_primary)
2062+
CacheInvalidateRelcache(heapRelation);
2063+
20532064
InvokeObjectPostAlterHookArg(IndexRelationId, indexRelationId, 0,
20542065
InvalidOid, is_internal);
20552066
}

src/test/regress/expected/publication.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ Publications:
204204
"testpib_ins_trunct"
205205
"testpub_fortbl"
206206

207+
-- verify relation cache invalidation when a primary key is added using
208+
-- an existing index
209+
CREATE TABLE pub_test.testpub_addpk (id int not null, data int);
210+
ALTER PUBLICATION testpub_default ADD TABLE pub_test.testpub_addpk;
211+
INSERT INTO pub_test.testpub_addpk VALUES(1, 11);
212+
CREATE UNIQUE INDEX testpub_addpk_id_idx ON pub_test.testpub_addpk(id);
213+
-- fail:
214+
UPDATE pub_test.testpub_addpk SET id = 2;
215+
ERROR: cannot update table "testpub_addpk" because it does not have a replica identity and publishes updates
216+
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.
217+
ALTER TABLE pub_test.testpub_addpk ADD PRIMARY KEY USING INDEX testpub_addpk_id_idx;
218+
-- now it should work:
219+
UPDATE pub_test.testpub_addpk SET id = 2;
220+
DROP TABLE pub_test.testpub_addpk;
207221
-- permissions
208222
SET ROLE regress_publication_user2;
209223
CREATE PUBLICATION testpub2; -- fail

src/test/regress/sql/publication.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ ALTER PUBLICATION testpub_default DROP TABLE pub_test.testpub_nopk;
105105

106106
\d+ testpub_tbl1
107107

108+
-- verify relation cache invalidation when a primary key is added using
109+
-- an existing index
110+
CREATE TABLE pub_test.testpub_addpk (id int not null, data int);
111+
ALTER PUBLICATION testpub_default ADD TABLE pub_test.testpub_addpk;
112+
INSERT INTO pub_test.testpub_addpk VALUES(1, 11);
113+
CREATE UNIQUE INDEX testpub_addpk_id_idx ON pub_test.testpub_addpk(id);
114+
-- fail:
115+
UPDATE pub_test.testpub_addpk SET id = 2;
116+
ALTER TABLE pub_test.testpub_addpk ADD PRIMARY KEY USING INDEX testpub_addpk_id_idx;
117+
-- now it should work:
118+
UPDATE pub_test.testpub_addpk SET id = 2;
119+
DROP TABLE pub_test.testpub_addpk;
120+
108121
-- permissions
109122
SET ROLE regress_publication_user2;
110123
CREATE PUBLICATION testpub2; -- fail

0 commit comments

Comments
 (0)