| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test the CREATE SUBSCRIPTION 'origin' parameter and its interaction with |
| 5 |
# 'copy_data' parameter. |
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
use PostgreSQL::Test::Cluster; |
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
my $tab_unquoted = q{tab'le}; |
| 13 |
my $tab = qq{"$tab_unquoted"}; |
| 14 |
|
| 15 |
my $subname_AB = 'tap_sub_A_B'; |
| 16 |
my $subname_AB2 = 'tap_sub_A_B_2'; |
| 17 |
my $subname_BA = 'tap_sub_B_A'; |
| 18 |
my $subname_BC = 'tap_sub_B_C'; |
| 19 |
|
| 20 |
my $result; |
| 21 |
my $stdout; |
| 22 |
my $stderr; |
| 23 |
|
| 24 |
############################################################################### |
| 25 |
# Setup a bidirectional logical replication between node_A & node_B |
| 26 |
############################################################################### |
| 27 |
|
| 28 |
# Initialize nodes |
| 29 |
# node_A |
| 30 |
my $node_A = PostgreSQL::Test::Cluster->new('node_A'); |
| 31 |
$node_A->init(allows_streaming => 'logical'); |
| 32 |
$node_A->start; |
| 33 |
|
| 34 |
# node_B |
| 35 |
my $node_B = PostgreSQL::Test::Cluster->new('node_B'); |
| 36 |
$node_B->init(allows_streaming => 'logical'); |
| 37 |
|
| 38 |
# Enable the track_commit_timestamp to detect the conflict when attempting to |
| 39 |
# update a row that was previously modified by a different origin. |
| 40 |
$node_B->append_conf('postgresql.conf', 'track_commit_timestamp = on'); |
| 41 |
$node_B->start; |
| 42 |
|
| 43 |
# Create table on node_A |
| 44 |
$node_A->safe_psql('postgres', "CREATE TABLE $tab (a int PRIMARY KEY)"); |
| 45 |
|
| 46 |
# Create the same table on node_B |
| 47 |
$node_B->safe_psql('postgres', "CREATE TABLE $tab (a int PRIMARY KEY)"); |
| 48 |
|
| 49 |
# Setup logical replication |
| 50 |
# node_A (pub) -> node_B (sub) |
| 51 |
my $node_A_connstr = $node_A->connstr . ' dbname=postgres'; |
| 52 |
$node_A->safe_psql('postgres', "CREATE PUBLICATION tap_pub_A FOR TABLE $tab"); |
| 53 |
$node_B->safe_psql( |
| 54 |
'postgres', " |
| 55 |
CREATE SUBSCRIPTION $subname_BA |
| 56 |
CONNECTION '$node_A_connstr application_name=$subname_BA' |
| 57 |
PUBLICATION tap_pub_A |
| 58 |
WITH (origin = none)"); |
| 59 |
|
| 60 |
# node_B (pub) -> node_A (sub) |
| 61 |
my $node_B_connstr = $node_B->connstr . ' dbname=postgres'; |
| 62 |
$node_B->safe_psql('postgres', "CREATE PUBLICATION tap_pub_B FOR TABLE $tab"); |
| 63 |
$node_A->safe_psql( |
| 64 |
'postgres', " |
| 65 |
CREATE SUBSCRIPTION $subname_AB |
| 66 |
CONNECTION '$node_B_connstr application_name=$subname_AB' |
| 67 |
PUBLICATION tap_pub_B |
| 68 |
WITH (origin = none, copy_data = off)"); |
| 69 |
|
| 70 |
# Wait for initial table sync to finish |
| 71 |
$node_A->wait_for_subscription_sync($node_B, $subname_AB); |
| 72 |
$node_B->wait_for_subscription_sync($node_A, $subname_BA); |
| 73 |
|
| 74 |
is(1, 1, 'Bidirectional replication setup is complete'); |
| 75 |
|
| 76 |
############################################################################### |
| 77 |
# Check that bidirectional logical replication setup does not cause infinite |
| 78 |
# recursive insertion. |
| 79 |
############################################################################### |
| 80 |
|
| 81 |
# insert a record |
| 82 |
$node_A->safe_psql('postgres', "INSERT INTO $tab VALUES (11);"); |
| 83 |
$node_B->safe_psql('postgres', "INSERT INTO $tab VALUES (21);"); |
| 84 |
|
| 85 |
$node_A->wait_for_catchup($subname_BA); |
| 86 |
$node_B->wait_for_catchup($subname_AB); |
| 87 |
|
| 88 |
# check that transaction was committed on subscriber(s) |
| 89 |
$result = $node_A->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 90 |
is( $result, qq(11 |
| 91 |
21), |
| 92 |
'Inserted successfully without leading to infinite recursion in bidirectional replication setup' |
| 93 |
); |
| 94 |
$result = $node_B->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 95 |
is( $result, qq(11 |
| 96 |
21), |
| 97 |
'Inserted successfully without leading to infinite recursion in bidirectional replication setup' |
| 98 |
); |
| 99 |
|
| 100 |
$node_A->safe_psql('postgres', "DELETE FROM $tab;"); |
| 101 |
|
| 102 |
$node_A->wait_for_catchup($subname_BA); |
| 103 |
$node_B->wait_for_catchup($subname_AB); |
| 104 |
|
| 105 |
############################################################################### |
| 106 |
# Check that remote data of node_B (that originated from node_C) is not |
| 107 |
# published to node_A. |
| 108 |
############################################################################### |
| 109 |
$result = $node_A->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 110 |
is($result, qq(), 'Check existing data'); |
| 111 |
|
| 112 |
$result = $node_B->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 113 |
is($result, qq(), 'Check existing data'); |
| 114 |
|
| 115 |
# Initialize node node_C |
| 116 |
my $node_C = PostgreSQL::Test::Cluster->new('node_C'); |
| 117 |
$node_C->init(allows_streaming => 'logical'); |
| 118 |
$node_C->start; |
| 119 |
|
| 120 |
$node_C->safe_psql('postgres', "CREATE TABLE $tab (a int PRIMARY KEY)"); |
| 121 |
|
| 122 |
# Setup logical replication |
| 123 |
# node_C (pub) -> node_B (sub) |
| 124 |
my $node_C_connstr = $node_C->connstr . ' dbname=postgres'; |
| 125 |
$node_C->safe_psql('postgres', "CREATE PUBLICATION tap_pub_C FOR TABLE $tab"); |
| 126 |
$node_B->safe_psql( |
| 127 |
'postgres', " |
| 128 |
CREATE SUBSCRIPTION $subname_BC |
| 129 |
CONNECTION '$node_C_connstr application_name=$subname_BC' |
| 130 |
PUBLICATION tap_pub_C |
| 131 |
WITH (origin = none)"); |
| 132 |
$node_B->wait_for_subscription_sync($node_C, $subname_BC); |
| 133 |
|
| 134 |
# insert a record |
| 135 |
$node_C->safe_psql('postgres', "INSERT INTO $tab VALUES (32);"); |
| 136 |
|
| 137 |
$node_C->wait_for_catchup($subname_BC); |
| 138 |
$node_B->wait_for_catchup($subname_AB); |
| 139 |
$node_A->wait_for_catchup($subname_BA); |
| 140 |
|
| 141 |
$result = $node_B->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 142 |
is($result, qq(32), 'The node_C data replicated to node_B'); |
| 143 |
|
| 144 |
# check that the data published from node_C to node_B is not sent to node_A |
| 145 |
$result = $node_A->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 146 |
is($result, qq(), |
| 147 |
'Remote data originating from another node (not the publisher) is not replicated when origin parameter is none' |
| 148 |
); |
| 149 |
|
| 150 |
############################################################################### |
| 151 |
# Check that the conflict can be detected when attempting to update or |
| 152 |
# delete a row that was previously modified by a different source. |
| 153 |
############################################################################### |
| 154 |
|
| 155 |
$node_B->safe_psql('postgres', "DELETE FROM $tab;"); |
| 156 |
|
| 157 |
$node_A->safe_psql('postgres', "INSERT INTO $tab VALUES (32);"); |
| 158 |
|
| 159 |
$node_A->wait_for_catchup($subname_BA); |
| 160 |
$node_B->wait_for_catchup($subname_AB); |
| 161 |
|
| 162 |
$result = $node_B->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 163 |
is($result, qq(32), 'The node_A data replicated to node_B'); |
| 164 |
|
| 165 |
# The update should update the row on node B that was inserted by node A. |
| 166 |
$node_C->safe_psql('postgres', "UPDATE $tab SET a = 33 WHERE a = 32;"); |
| 167 |
|
| 168 |
$node_B->wait_for_log( |
| 169 |
qr/conflict detected on relation "public.$tab_unquoted": conflict=update_origin_differs.*\n.*DETAIL:.* Updating the row that was modified by a different origin ".*" in transaction [0-9]+ at .*\n.*Existing local row \(32\); remote row \(33\); replica identity \(a\)=\(32\)/ |
| 170 |
); |
| 171 |
|
| 172 |
$node_B->safe_psql('postgres', "DELETE FROM $tab;"); |
| 173 |
$node_A->safe_psql('postgres', "INSERT INTO $tab VALUES (33);"); |
| 174 |
|
| 175 |
$node_A->wait_for_catchup($subname_BA); |
| 176 |
$node_B->wait_for_catchup($subname_AB); |
| 177 |
|
| 178 |
$result = $node_B->safe_psql('postgres', "SELECT * FROM $tab ORDER BY 1;"); |
| 179 |
is($result, qq(33), 'The node_A data replicated to node_B'); |
| 180 |
|
| 181 |
# The delete should remove the row on node B that was inserted by node A. |
| 182 |
$node_C->safe_psql('postgres', "DELETE FROM $tab WHERE a = 33;"); |
| 183 |
|
| 184 |
$node_B->wait_for_log( |
| 185 |
qr/conflict detected on relation "public.$tab_unquoted": conflict=delete_origin_differs.*\n.*DETAIL:.* Deleting the row that was modified by a different origin ".*" in transaction [0-9]+ at .*\n.*Existing local row \(33\); replica identity \(a\)=\(33\)/ |
| 186 |
); |
| 187 |
|
| 188 |
# The remaining tests no longer test conflict detection. |
| 189 |
$node_B->append_conf('postgresql.conf', 'track_commit_timestamp = off'); |
| 190 |
$node_B->restart; |
| 191 |
|
| 192 |
############################################################################### |
| 193 |
# Specifying origin = NONE indicates that the publisher should only replicate the |
| 194 |
# changes that are generated locally from node_B, but in this case since the |
| 195 |
# node_B is also subscribing data from node_A, node_B can have remotely |
| 196 |
# originated data from node_A. We log a warning, in this case, to draw |
| 197 |
# attention to there being possible remote data. |
| 198 |
############################################################################### |
| 199 |
($result, $stdout, $stderr) = $node_A->psql( |
| 200 |
'postgres', " |
| 201 |
CREATE SUBSCRIPTION $subname_AB2 |
| 202 |
CONNECTION '$node_B_connstr application_name=$subname_AB2' |
| 203 |
PUBLICATION tap_pub_B |
| 204 |
WITH (origin = none, copy_data = on)"); |
| 205 |
like( |
| 206 |
$stderr, |
| 207 |
qr/WARNING: ( [A-Z0-9]+:)? subscription "tap_sub_a_b_2" requested copy_data with origin = NONE but might copy data that had a different origin/, |
| 208 |
"Create subscription with origin = none and copy_data when the publisher has subscribed same table" |
| 209 |
); |
| 210 |
|
| 211 |
$node_A->wait_for_subscription_sync($node_B, $subname_AB2); |
| 212 |
|
| 213 |
# Alter subscription ... refresh publication should be successful when no new |
| 214 |
# table is added |
| 215 |
$node_A->safe_psql( |
| 216 |
'postgres', " |
| 217 |
ALTER SUBSCRIPTION $subname_AB2 REFRESH PUBLICATION"); |
| 218 |
|
| 219 |
# Check Alter subscription ... refresh publication when there is a new |
| 220 |
# table that is subscribing data from a different publication |
| 221 |
$node_A->safe_psql('postgres', "CREATE TABLE tab_new (a int PRIMARY KEY)"); |
| 222 |
$node_B->safe_psql('postgres', "CREATE TABLE tab_new (a int PRIMARY KEY)"); |
| 223 |
|
| 224 |
# add a new table to the publication |
| 225 |
$node_A->safe_psql('postgres', |
| 226 |
"ALTER PUBLICATION tap_pub_A ADD TABLE tab_new"); |
| 227 |
$node_B->safe_psql( |
| 228 |
'postgres', " |
| 229 |
ALTER SUBSCRIPTION $subname_BA REFRESH PUBLICATION"); |
| 230 |
|
| 231 |
$node_B->wait_for_subscription_sync($node_A, $subname_BA); |
| 232 |
|
| 233 |
# add a new table to the publication |
| 234 |
$node_B->safe_psql('postgres', |
| 235 |
"ALTER PUBLICATION tap_pub_B ADD TABLE tab_new"); |
| 236 |
|
| 237 |
# Alter subscription ... refresh publication should log a warning when a new |
| 238 |
# table on the publisher is subscribing data from a different publication |
| 239 |
($result, $stdout, $stderr) = $node_A->psql( |
| 240 |
'postgres', " |
| 241 |
ALTER SUBSCRIPTION $subname_AB2 REFRESH PUBLICATION"); |
| 242 |
like( |
| 243 |
$stderr, |
| 244 |
qr/WARNING: ( [A-Z0-9]+:)? subscription "tap_sub_a_b_2" requested copy_data with origin = NONE but might copy data that had a different origin/, |
| 245 |
"Refresh publication when the publisher has subscribed for the new table, but the subscriber-side wants origin = none" |
| 246 |
); |
| 247 |
|
| 248 |
# Ensure that relation has reached 'ready' state before we try to drop it |
| 249 |
my $synced_query = |
| 250 |
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r');"; |
| 251 |
$node_A->poll_query_until('postgres', $synced_query) |
| 252 |
or die "Timed out while waiting for subscriber to synchronize data"; |
| 253 |
|
| 254 |
$node_B->wait_for_catchup($subname_AB2); |
| 255 |
|
| 256 |
# clear the operations done by this test |
| 257 |
$node_A->safe_psql( |
| 258 |
'postgres', qq( |
| 259 |
DROP TABLE tab_new; |
| 260 |
DROP SUBSCRIPTION $subname_AB2; |
| 261 |
DROP SUBSCRIPTION $subname_AB; |
| 262 |
DROP PUBLICATION tap_pub_A; |
| 263 |
)); |
| 264 |
$node_B->safe_psql( |
| 265 |
'postgres', qq( |
| 266 |
DROP TABLE tab_new; |
| 267 |
DROP SUBSCRIPTION $subname_BA; |
| 268 |
DROP PUBLICATION tap_pub_B; |
| 269 |
)); |
| 270 |
|
| 271 |
############################################################################### |
| 272 |
# Specifying origin = NONE and copy_data = on must raise WARNING if we subscribe |
| 273 |
# to a partitioned table and this table contains any remotely originated data. |
| 274 |
# |
| 275 |
# node_B |
| 276 |
# __________________________ |
| 277 |
# | tab_main | --------------> node_C (tab_main) |
| 278 |
# |__________________________| |
| 279 |
# | tab_part1 | tab_part2 | <-------------- node_A (tab_part2) |
| 280 |
# |____________|_____________| |
| 281 |
# | tab_part2_1 | |
| 282 |
# |_____________| |
| 283 |
# |
| 284 |
# node_B |
| 285 |
# __________________________ |
| 286 |
# | tab_main | |
| 287 |
# |__________________________| |
| 288 |
# | tab_part1 | tab_part2 | <-------------- node_A (tab_part2) |
| 289 |
# |____________|_____________| |
| 290 |
# | tab_part2_1 | --------------> node_C (tab_part2_1) |
| 291 |
# |_____________| |
| 292 |
############################################################################### |
| 293 |
|
| 294 |
# create a table on node A which will act as a source for a partition on node B |
| 295 |
$node_A->safe_psql( |
| 296 |
'postgres', qq( |
| 297 |
CREATE TABLE tab_part2(a int); |
| 298 |
CREATE PUBLICATION tap_pub_A FOR TABLE tab_part2; |
| 299 |
)); |
| 300 |
|
| 301 |
# create a partition table on node B |
| 302 |
$node_B->safe_psql( |
| 303 |
'postgres', qq( |
| 304 |
CREATE TABLE tab_main(a int) PARTITION BY RANGE(a); |
| 305 |
CREATE TABLE tab_part1 PARTITION OF tab_main FOR VALUES FROM (0) TO (5); |
| 306 |
CREATE TABLE tab_part2(a int) PARTITION BY RANGE(a); |
| 307 |
CREATE TABLE tab_part2_1 PARTITION OF tab_part2 FOR VALUES FROM (5) TO (10); |
| 308 |
ALTER TABLE tab_main ATTACH PARTITION tab_part2 FOR VALUES FROM (5) to (10); |
| 309 |
CREATE SUBSCRIPTION tap_sub_A_B CONNECTION '$node_A_connstr' PUBLICATION tap_pub_A; |
| 310 |
)); |
| 311 |
|
| 312 |
# create a table on node C |
| 313 |
$node_C->safe_psql( |
| 314 |
'postgres', qq( |
| 315 |
CREATE TABLE tab_main(a int); |
| 316 |
CREATE TABLE tab_part2_1(a int); |
| 317 |
)); |
| 318 |
|
| 319 |
# create a logical replication setup between node B and node C with |
| 320 |
# subscription on node C having origin = NONE and copy_data = on |
| 321 |
$node_B->safe_psql( |
| 322 |
'postgres', qq( |
| 323 |
CREATE PUBLICATION tap_pub_B FOR TABLE tab_main WITH (publish_via_partition_root); |
| 324 |
CREATE PUBLICATION tap_pub_B_2 FOR TABLE tab_part2_1; |
| 325 |
)); |
| 326 |
|
| 327 |
($result, $stdout, $stderr) = $node_C->psql( |
| 328 |
'postgres', " |
| 329 |
CREATE SUBSCRIPTION tap_sub_B_C CONNECTION '$node_B_connstr' PUBLICATION tap_pub_B WITH (origin = none, copy_data = on); |
| 330 |
"); |
| 331 |
|
| 332 |
# A warning must be logged as a partition 'tab_part2' in node B is subscribed to |
| 333 |
# node A so partition 'tab_part2' can have remotely originated data |
| 334 |
like( |
| 335 |
$stderr, |
| 336 |
qr/WARNING: ( [A-Z0-9]+:)? subscription "tap_sub_b_c" requested copy_data with origin = NONE but might copy data that had a different origin/, |
| 337 |
"Create subscription with origin = none and copy_data when the publisher's partition is subscribing from different origin" |
| 338 |
); |
| 339 |
$node_C->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_B_C"); |
| 340 |
|
| 341 |
($result, $stdout, $stderr) = $node_C->psql( |
| 342 |
'postgres', " |
| 343 |
CREATE SUBSCRIPTION tap_sub_B_C CONNECTION '$node_B_connstr' PUBLICATION tap_pub_B_2 WITH (origin = none, copy_data = on); |
| 344 |
"); |
| 345 |
|
| 346 |
# A warning must be logged as ancestor of table 'tab_part2_1' in node B is |
| 347 |
# subscribed to node A so table 'tab_part2_1' can have remotely originated |
| 348 |
# data |
| 349 |
like( |
| 350 |
$stderr, |
| 351 |
qr/WARNING: ( [A-Z0-9]+:)? subscription "tap_sub_b_c" requested copy_data with origin = NONE but might copy data that had a different origin/, |
| 352 |
"Create subscription with origin = none and copy_data when the publisher's ancestor is subscribing from different origin" |
| 353 |
); |
| 354 |
|
| 355 |
# clear the operations done by this test |
| 356 |
$node_C->safe_psql( |
| 357 |
'postgres', qq( |
| 358 |
DROP SUBSCRIPTION tap_sub_B_C; |
| 359 |
DROP TABLE tab_main; |
| 360 |
DROP TABLE tab_part2_1; |
| 361 |
)); |
| 362 |
$node_B->safe_psql( |
| 363 |
'postgres', qq( |
| 364 |
DROP SUBSCRIPTION tap_sub_A_B; |
| 365 |
DROP PUBLICATION tap_pub_B; |
| 366 |
DROP PUBLICATION tap_pub_B_2; |
| 367 |
DROP TABLE tab_main; |
| 368 |
)); |
| 369 |
$node_A->safe_psql( |
| 370 |
'postgres', qq( |
| 371 |
DROP PUBLICATION tap_pub_A; |
| 372 |
DROP TABLE tab_part2; |
| 373 |
)); |
| 374 |
|
| 375 |
# shutdown |
| 376 |
$node_B->stop('fast'); |
| 377 |
$node_A->stop('fast'); |
| 378 |
$node_C->stop('fast'); |
| 379 |
|
| 380 |
done_testing(); |