| 1 |
# Copyright (c) 2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Test the conflict detection of conflict type 'multiple_unique_conflicts'. |
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
|
| 10 |
############################### |
| 11 |
# Setup |
| 12 |
############################### |
| 13 |
|
| 14 |
# Create a publisher node |
| 15 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 16 |
$node_publisher->init(allows_streaming => 'logical'); |
| 17 |
$node_publisher->start; |
| 18 |
|
| 19 |
# Create a subscriber node |
| 20 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 21 |
$node_subscriber->init; |
| 22 |
$node_subscriber->start; |
| 23 |
|
| 24 |
# Create a table on publisher |
| 25 |
$node_publisher->safe_psql('postgres', |
| 26 |
"CREATE TABLE conf_tab (a int PRIMARY KEY, b int UNIQUE, c int UNIQUE);"); |
| 27 |
|
| 28 |
$node_publisher->safe_psql('postgres', |
| 29 |
"CREATE TABLE conf_tab_2 (a int PRIMARY KEY, b int UNIQUE, c int UNIQUE);" |
| 30 |
); |
| 31 |
|
| 32 |
# Create same table on subscriber |
| 33 |
$node_subscriber->safe_psql('postgres', |
| 34 |
"CREATE TABLE conf_tab (a int PRIMARY key, b int UNIQUE, c int UNIQUE);"); |
| 35 |
|
| 36 |
$node_subscriber->safe_psql( |
| 37 |
'postgres', qq[ |
| 38 |
CREATE TABLE conf_tab_2 (a int PRIMARY KEY, b int, c int, unique(a,b)) PARTITION BY RANGE (a); |
| 39 |
CREATE TABLE conf_tab_2_p1 PARTITION OF conf_tab_2 FOR VALUES FROM (MINVALUE) TO (100); |
| 40 |
]); |
| 41 |
|
| 42 |
# Setup logical replication |
| 43 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 44 |
$node_publisher->safe_psql('postgres', |
| 45 |
"CREATE PUBLICATION pub_tab FOR TABLE conf_tab, conf_tab_2"); |
| 46 |
|
| 47 |
# Create the subscription |
| 48 |
my $appname = 'sub_tab'; |
| 49 |
$node_subscriber->safe_psql( |
| 50 |
'postgres', |
| 51 |
"CREATE SUBSCRIPTION sub_tab |
| 52 |
CONNECTION '$publisher_connstr application_name=$appname' |
| 53 |
PUBLICATION pub_tab;"); |
| 54 |
|
| 55 |
# Wait for initial table sync to finish |
| 56 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 57 |
|
| 58 |
################################################## |
| 59 |
# INSERT data on Pub and Sub |
| 60 |
################################################## |
| 61 |
|
| 62 |
# Insert data in the publisher table |
| 63 |
$node_publisher->safe_psql('postgres', |
| 64 |
"INSERT INTO conf_tab VALUES (1,1,1);"); |
| 65 |
|
| 66 |
# Insert data in the subscriber table |
| 67 |
$node_subscriber->safe_psql('postgres', |
| 68 |
"INSERT INTO conf_tab VALUES (2,2,2), (3,3,3), (4,4,4);"); |
| 69 |
|
| 70 |
################################################## |
| 71 |
# Test multiple_unique_conflicts due to INSERT |
| 72 |
################################################## |
| 73 |
my $log_offset = -s $node_subscriber->logfile; |
| 74 |
|
| 75 |
$node_publisher->safe_psql('postgres', |
| 76 |
"INSERT INTO conf_tab VALUES (2,3,4);"); |
| 77 |
|
| 78 |
# Confirm that this causes an error on the subscriber |
| 79 |
$node_subscriber->wait_for_log( |
| 80 |
qr/conflict detected on relation \"public.conf_tab\": conflict=multiple_unique_conflicts.* |
| 81 |
.*Key already exists in unique index \"conf_tab_pkey\".* |
| 82 |
.*Key \(a\)=\(2\); existing local row \(2, 2, 2\); remote row \(2, 3, 4\).* |
| 83 |
.*Key already exists in unique index \"conf_tab_b_key\".* |
| 84 |
.*Key \(b\)=\(3\); existing local row \(3, 3, 3\); remote row \(2, 3, 4\).* |
| 85 |
.*Key already exists in unique index \"conf_tab_c_key\".* |
| 86 |
.*Key \(c\)=\(4\); existing local row \(4, 4, 4\); remote row \(2, 3, 4\)./, |
| 87 |
$log_offset); |
| 88 |
|
| 89 |
pass('multiple_unique_conflicts detected during insert'); |
| 90 |
|
| 91 |
# Truncate table to get rid of the error |
| 92 |
$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); |
| 93 |
|
| 94 |
################################################## |
| 95 |
# Test multiple_unique_conflicts due to UPDATE |
| 96 |
################################################## |
| 97 |
$log_offset = -s $node_subscriber->logfile; |
| 98 |
|
| 99 |
# Insert data in the publisher table |
| 100 |
$node_publisher->safe_psql('postgres', |
| 101 |
"INSERT INTO conf_tab VALUES (5,5,5);"); |
| 102 |
|
| 103 |
# Insert data in the subscriber table |
| 104 |
$node_subscriber->safe_psql('postgres', |
| 105 |
"INSERT INTO conf_tab VALUES (6,6,6), (7,7,7), (8,8,8);"); |
| 106 |
|
| 107 |
$node_publisher->safe_psql('postgres', |
| 108 |
"UPDATE conf_tab set a=6, b=7, c=8 where a=5;"); |
| 109 |
|
| 110 |
# Confirm that this causes an error on the subscriber |
| 111 |
$node_subscriber->wait_for_log( |
| 112 |
qr/conflict detected on relation \"public.conf_tab\": conflict=multiple_unique_conflicts.* |
| 113 |
.*Key already exists in unique index \"conf_tab_pkey\".* |
| 114 |
.*Key \(a\)=\(6\); existing local row \(6, 6, 6\); remote row \(6, 7, 8\).* |
| 115 |
.*Key already exists in unique index \"conf_tab_b_key\".* |
| 116 |
.*Key \(b\)=\(7\); existing local row \(7, 7, 7\); remote row \(6, 7, 8\).* |
| 117 |
.*Key already exists in unique index \"conf_tab_c_key\".* |
| 118 |
.*Key \(c\)=\(8\); existing local row \(8, 8, 8\); remote row \(6, 7, 8\)./, |
| 119 |
$log_offset); |
| 120 |
|
| 121 |
pass('multiple_unique_conflicts detected during update'); |
| 122 |
|
| 123 |
# Truncate table to get rid of the error |
| 124 |
$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); |
| 125 |
|
| 126 |
|
| 127 |
################################################## |
| 128 |
# Test multiple_unique_conflicts due to INSERT on a leaf partition |
| 129 |
################################################## |
| 130 |
|
| 131 |
# Insert data in the subscriber table |
| 132 |
$node_subscriber->safe_psql('postgres', |
| 133 |
"INSERT INTO conf_tab_2 VALUES (55,2,3);"); |
| 134 |
|
| 135 |
# Insert data in the publisher table |
| 136 |
$node_publisher->safe_psql('postgres', |
| 137 |
"INSERT INTO conf_tab_2 VALUES (55,2,3);"); |
| 138 |
|
| 139 |
$node_subscriber->wait_for_log( |
| 140 |
qr/conflict detected on relation \"public.conf_tab_2_p1\": conflict=multiple_unique_conflicts.* |
| 141 |
.*Key already exists in unique index \"conf_tab_2_p1_pkey\".* |
| 142 |
.*Key \(a\)=\(55\); existing local row \(55, 2, 3\); remote row \(55, 2, 3\).* |
| 143 |
.*Key already exists in unique index \"conf_tab_2_p1_a_b_key\".* |
| 144 |
.*Key \(a, b\)=\(55, 2\); existing local row \(55, 2, 3\); remote row \(55, 2, 3\)./, |
| 145 |
$log_offset); |
| 146 |
|
| 147 |
pass('multiple_unique_conflicts detected on a leaf partition during insert'); |
| 148 |
|
| 149 |
done_testing(); |