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

Commit31b7b4d

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 parent64ebb43 commit31b7b4d

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
@@ -2046,6 +2046,7 @@ index_constraint_create(Relation heapRelation,
20462046
HeapTupleindexTuple;
20472047
Form_pg_indexindexForm;
20482048
booldirty= false;
2049+
boolmarked_as_primary= false;
20492050

20502051
pg_index=table_open(IndexRelationId,RowExclusiveLock);
20512052

@@ -2059,6 +2060,7 @@ index_constraint_create(Relation heapRelation,
20592060
{
20602061
indexForm->indisprimary= true;
20612062
dirty= true;
2063+
marked_as_primary= true;
20622064
}
20632065

20642066
if (deferrable&&indexForm->indimmediate)
@@ -2071,6 +2073,15 @@ index_constraint_create(Relation heapRelation,
20712073
{
20722074
CatalogTupleUpdate(pg_index,&indexTuple->t_self,indexTuple);
20732075

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

‎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