Skip to content

Commit 40fb634

Browse files
author
Amit Kapila
committed
Invalidate relcache when changing REPLICA IDENTITY index.
When changing REPLICA IDENTITY INDEX to another one, the target table's relcache was not being invalidated. This leads to skipping update/delete operations during apply on the subscriber side as the columns required to search corresponding rows won't get logged. Author: Tang Haiying, Hou Zhijie Reviewed-by: Euler Taveira, Amit Kapila Backpatch-through: 10 Discussion: https://postgr.es/m/OS0PR01MB61133CA11630DAE45BC6AD95FB939@OS0PR01MB6113.jpnprd01.prod.outlook.com
1 parent b062ca5 commit 40fb634

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/backend/commands/tablecmds.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13398,6 +13398,12 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
1339813398
CatalogTupleUpdate(pg_index, &pg_index_tuple->t_self, pg_index_tuple);
1339913399
InvokeObjectPostAlterHookArg(IndexRelationId, thisIndexOid, 0,
1340013400
InvalidOid, is_internal);
13401+
/*
13402+
* Invalidate the relcache for the table, so that after we commit
13403+
* all sessions will refresh the table's replica identity index
13404+
* before attempting any UPDATE or DELETE on the table.
13405+
*/
13406+
CacheInvalidateRelcache(rel);
1340113407
}
1340213408
heap_freetuple(pg_index_tuple);
1340313409
}

src/test/subscription/t/100_bugs.pl

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use warnings;
44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 3;
6+
use Test::More tests => 5;
77

88
# Bug #15114
99

@@ -98,3 +98,87 @@
9898
);
9999

100100
$node_publisher->stop('fast');
101+
102+
# https://postgr.es/m/OS0PR01MB61133CA11630DAE45BC6AD95FB939%40OS0PR01MB6113.jpnprd01.prod.outlook.com
103+
104+
# The bug was that when changing the REPLICA IDENTITY INDEX to another one, the
105+
# target table's relcache was not being invalidated. This leads to skipping
106+
# UPDATE/DELETE operations during apply on the subscriber side as the columns
107+
# required to search corresponding rows won't get logged.
108+
$node_publisher = get_new_node('publisher3');
109+
$node_publisher->init(allows_streaming => 'logical');
110+
$node_publisher->start;
111+
112+
$node_subscriber = get_new_node('subscriber3');
113+
$node_subscriber->init(allows_streaming => 'logical');
114+
$node_subscriber->start;
115+
116+
$node_publisher->safe_psql('postgres',
117+
"CREATE TABLE tab_replidentity_index(a int not null, b int not null)");
118+
$node_publisher->safe_psql('postgres',
119+
"CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)"
120+
);
121+
$node_publisher->safe_psql('postgres',
122+
"CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)"
123+
);
124+
125+
# use index idx_replidentity_index_a as REPLICA IDENTITY on publisher.
126+
$node_publisher->safe_psql('postgres',
127+
"ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_a"
128+
);
129+
130+
$node_publisher->safe_psql('postgres',
131+
"INSERT INTO tab_replidentity_index VALUES(1, 1),(2, 2)");
132+
133+
$node_subscriber->safe_psql('postgres',
134+
"CREATE TABLE tab_replidentity_index(a int not null, b int not null)");
135+
$node_subscriber->safe_psql('postgres',
136+
"CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)"
137+
);
138+
$node_subscriber->safe_psql('postgres',
139+
"CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)"
140+
);
141+
# use index idx_replidentity_index_b as REPLICA IDENTITY on subscriber because
142+
# it reflects the future scenario we are testing: changing REPLICA IDENTITY
143+
# INDEX.
144+
$node_subscriber->safe_psql('postgres',
145+
"ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b"
146+
);
147+
148+
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
149+
$node_publisher->safe_psql('postgres',
150+
"CREATE PUBLICATION tap_pub FOR TABLE tab_replidentity_index");
151+
$node_subscriber->safe_psql('postgres',
152+
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=tap_sub' PUBLICATION tap_pub"
153+
);
154+
155+
$node_publisher->wait_for_catchup('tap_sub');
156+
157+
# Also wait for initial table sync to finish
158+
my $synced_query =
159+
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
160+
$node_subscriber->poll_query_until('postgres', $synced_query)
161+
or die "Timed out while waiting for subscriber to synchronize data";
162+
163+
is( $node_subscriber->safe_psql(
164+
'postgres', "SELECT * FROM tab_replidentity_index"),
165+
qq(1|1
166+
2|2),
167+
"check initial data on subscriber");
168+
169+
# Set REPLICA IDENTITY to idx_replidentity_index_b on publisher, then run UPDATE and DELETE.
170+
$node_publisher->safe_psql(
171+
'postgres', qq[
172+
ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b;
173+
UPDATE tab_replidentity_index SET a = -a WHERE a = 1;
174+
DELETE FROM tab_replidentity_index WHERE a = 2;
175+
]);
176+
177+
$node_publisher->wait_for_catchup('tap_sub');
178+
is( $node_subscriber->safe_psql(
179+
'postgres', "SELECT * FROM tab_replidentity_index"),
180+
qq(-1|1),
181+
"update works with REPLICA IDENTITY");
182+
183+
$node_publisher->stop('fast');
184+
$node_subscriber->stop('fast');

0 commit comments

Comments
 (0)