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

Commit3839e29

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 theexistence of a primary key: other sessions (and ours too for thatmatter) failed to recalculate their opinion of whether the table canbe 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 causea cache flush. Hence, backpatch to v11.Report and patch by Hou ZhijieDiscussion:https://postgr.es/m/OS0PR01MB5716EBE01F112C62F8F9B786947B9@OS0PR01MB5716.jpnprd01.prod.outlook.com
1 parentf4ebf0d commit3839e29

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
@@ -2050,6 +2050,7 @@ index_constraint_create(Relation heapRelation,
20502050
HeapTupleindexTuple;
20512051
Form_pg_indexindexForm;
20522052
booldirty= false;
2053+
boolmarked_as_primary= false;
20532054

20542055
pg_index=table_open(IndexRelationId,RowExclusiveLock);
20552056

@@ -2063,6 +2064,7 @@ index_constraint_create(Relation heapRelation,
20632064
{
20642065
indexForm->indisprimary= true;
20652066
dirty= true;
2067+
marked_as_primary= true;
20662068
}
20672069

20682070
if (deferrable&&indexForm->indimmediate)
@@ -2075,6 +2077,15 @@ index_constraint_create(Relation heapRelation,
20752077
{
20762078
CatalogTupleUpdate(pg_index,&indexTuple->t_self,indexTuple);
20772079

2080+
/*
2081+
* When we mark an existing index as primary, force a relcache
2082+
* flush on its parent table, so that all sessions will become
2083+
* aware that the table now has a primary key. This is important
2084+
* because it affects some replication behaviors.
2085+
*/
2086+
if (marked_as_primary)
2087+
CacheInvalidateRelcache(heapRelation);
2088+
20782089
InvokeObjectPostAlterHookArg(IndexRelationId,indexRelationId,0,
20792090
InvalidOid,is_internal);
20802091
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ Publications:
268268
"testpib_ins_trunct"
269269
"testpub_fortbl"
270270

271+
-- verify relation cache invalidation when a primary key is added using
272+
-- an existing index
273+
CREATE TABLE pub_test.testpub_addpk (id int not null, data int);
274+
ALTER PUBLICATION testpub_default ADD TABLE pub_test.testpub_addpk;
275+
INSERT INTO pub_test.testpub_addpk VALUES(1, 11);
276+
CREATE UNIQUE INDEX testpub_addpk_id_idx ON pub_test.testpub_addpk(id);
277+
-- fail:
278+
UPDATE pub_test.testpub_addpk SET id = 2;
279+
ERROR: cannot update table "testpub_addpk" because it does not have a replica identity and publishes updates
280+
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.
281+
ALTER TABLE pub_test.testpub_addpk ADD PRIMARY KEY USING INDEX testpub_addpk_id_idx;
282+
-- now it should work:
283+
UPDATE pub_test.testpub_addpk SET id = 2;
284+
DROP TABLE pub_test.testpub_addpk;
271285
-- permissions
272286
SET ROLE regress_publication_user2;
273287
CREATE PUBLICATION testpub2; -- fail

‎src/test/regress/sql/publication.sql‎

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

150150
\d+ testpub_tbl1
151151

152+
-- verify relation cache invalidation when a primary key is added using
153+
-- an existing index
154+
CREATETABLEpub_test.testpub_addpk (idintnot null, dataint);
155+
ALTER PUBLICATION testpub_default ADD TABLEpub_test.testpub_addpk;
156+
INSERT INTOpub_test.testpub_addpkVALUES(1,11);
157+
CREATEUNIQUE INDEXtestpub_addpk_id_idxONpub_test.testpub_addpk(id);
158+
-- fail:
159+
UPDATEpub_test.testpub_addpkSET id=2;
160+
ALTERTABLEpub_test.testpub_addpk ADDPRIMARY KEY USING INDEX testpub_addpk_id_idx;
161+
-- now it should work:
162+
UPDATEpub_test.testpub_addpkSET id=2;
163+
DROPTABLEpub_test.testpub_addpk;
164+
152165
-- permissions
153166
SET ROLE regress_publication_user2;
154167
CREATE PUBLICATION testpub2;-- fail

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp